1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Lí thuyết đồ thị part 10 pps

14 338 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 14
Dung lượng 97,72 KB

Nội dung

typedef byte SetOfVertices[(MAXVERTICES%8)?((MAXVERTICES/8)+1):(MAXVERTICES/8)]; /*********************************** Danh sach da lien ket cho cac canh ***********************************/ typedef struct EdgeNode *EdgePointer; struct EdgeNode { byte Vertex[2]; EdgePointer Link[2]; }; typedef struct { char Data; EdgePointer Next; } ListEdge; typedef ListEdge *ListEdgePointer; typedef ListEdgePointer ArrayOfEdge[MAXVERTICES]; /***** Phan khai bao prototype ham *****/ void Create(AdjPointer *List); Boolean Empty(AdjPointer List); void Push(AdjPointer *List, byte Item); void Pop(AdjPointer *List, byte *Item); void CreatQueue(Queue *Q); Boolean EmptyQueue(Queue Q); void PushQueue(Queue *Q, byte Item); void PopQueue(Queue *Q, byte *Item); Boolean EmptySet(SetOfVertices S); Boolean InSet(SetOfVertices S, byte Value); void InitSet(SetOfVertices S, byte MaxValue); void AddSet(SetOfVertices S, byte Value); void SubSet(SetOfVertices S, byte Value); void MakeV_out(char *FileName, ArrayOfPointer V_out, byte *NumVertices, 199 Boolean Weight); void MakeV_in(char *FileName, ArrayOfPointer V_in, ArrayOfPointer V_out, byte *NumVertices); void BuildGraph(char *FileName, ArrayOfEdge E, byte *NumVertices); void DisplayV_out(ArrayOfPointer V_out, byte NumVertices, Boolean Weight); void DisplayV_in(ArrayOfPointer V_in, byte NumVertices); void DFS(ArrayOfEdge E, byte Start, SetOfVertices S); void PathTwoVertex(Path Pred, byte Start, byte Terminal); void PopHeadPtr(byte NumVertices, ArrayOfPointer V_out); /***** Phan cai dat cac ham *****/ void Create(AdjPointer *List) { (*List) = NULL; } Boolean Empty(AdjPointer List) { return((List == NULL) ? TRUE : FALSE); } void Push(AdjPointer *List, byte Item) { AdjPointer TempPtr; TempPtr = (AdjPointer) malloc(sizeof(struct VertexNode)); TempPtr->Vertex = Item; TempPtr->Next = (*List); (*List) = TempPtr; } void Pop(AdjPointer *List, byte *Item) { AdjPointer TempPtr; if (Empty(*List)) { printf(" Thao tac con tro khong hop le "); return; 200 } TempPtr = (*List); (*Item) = TempPtr->Vertex; (*List) = TempPtr->Next; free(TempPtr); } void CreatQueue(Queue *Q) { (*Q).Head = NULL; } Boolean EmptyQueue(Queue Q) { return((Q.Head == NULL) ? TRUE : FALSE); } void PushQueue(Queue *Q, byte Item) { QueueNode TempPtr; TempPtr = (QueueNode) malloc(sizeof(struct QueueType)); TempPtr->Vertex = Item; TempPtr->Next = NULL; if ((*Q).Head == NULL) (*Q).Head = TempPtr; else (*Q).Tail->Next = TempPtr; (*Q).Tail = TempPtr; } void PopQueue(Queue *Q, byte *Item) { QueueNode TempPtr; if (EmptyQueue(*Q)) { printf(" Thao tac con tro khong hop le "); return; } TempPtr = (*Q).Head; (*Item) = TempPtr->Vertex; (*Q).Head = TempPtr->Next; free(TempPtr); 201 } Boolean EmptySet(SetOfVertices S) { int i, k = (MAXVERTICES %8 ) ? ((MAXVERTICES / 8) + 1) : (MAXVERTICES / 8); for (i = 0; i < k; i++) if (S[i] != 0) return(FALSE); return(TRUE); } Boolean InSet(SetOfVertices S, byte Value) { if ((Value < 1) || (Value > MAXVERTICES)) return(FALSE); return((S[(Value - 1) / 8] & (0x80 >> ((Value - 1) % 8))) ? TRUE : FALSE); } void InitSet(SetOfVertices S, byte MaxValue) { int i; if (MaxValue>MAXVERTICES) { printf(" Gia tri khong thuoc tap hop "); return; } for (i = 0; i < MaxValue; i++) S[i/8] |= 0x80 >> (i%8); } void AddSet(SetOfVertices S, byte Value) { int i; if ((Value < 1) || (Value > MAXVERTICES)) { printf(" Gia tri khong thuoc tap hop "); return; } 202 S[(Value-1)/8] |= 0x80 >> ((Value-1)%8); } void SubSet(SetOfVertices S, byte Value) { if ((Value < 1) || (Value > MAXVERTICES)) { printf(" Gia tri khong thuoc tap hop "); return; } S[(Value-1)/8] &= ~(0x80 >> ((Value-1)%8)); } int feoln(FILE *fp) { char c; if ((c=fgetc(fp))==10) { fseek(fp, -2, 1); return(TRUE); } else if (c==EOF) return(TRUE); else { fseek(fp, -1, 1); return(FALSE); } } void freadln(FILE *fp) { char c; while (((c=fgetc(fp))!=10)&&(c!=EOF)); } void MakeV_out(char *FileName, ArrayOfPointer V_out, byte *NumVertices, Boolean Weight) { 203 byte NumVert; HeadPointer HeadPtr; AdjPointer VerPtr; FILE *FileData; if ((FileData = fopen(FileName, "rt")) == NULL) { printf(" File khong tim thay "); return; } NumVert = 0; while (!feof(FileData)) { HeadPtr = (HeadPointer) malloc(sizeof(HeadNode)); HeadPtr->Next = NULL; fgets(HeadPtr->Data, MAXSTRINGS+1, FileData); // Ham fgets(char *s, int n, FILE *fp) chi doc n-1 ky tu while (!feoln(FileData)) { VerPtr = (AdjPointer) malloc(sizeof(struct VertexNode)); fscanf(FileData, "%d", &(VerPtr->Vertex)); if (Weight) { fscanf(FileData, "%d", &(VerPtr->Length)); VerPtr->Flow = 0; } VerPtr->Next = HeadPtr->Next; HeadPtr->Next = VerPtr; } freadln(FileData); ++NumVert; V_out[NumVert] = HeadPtr; } (*NumVertices) = NumVert; fclose(FileData); } void MakeV_in(char *FileName, ArrayOfPointer V_in, ArrayOfPointer V_out, byte *NumVertices); { byte NumVert; 204 int i, j; HeadPointer HeadPtr; AdjPointer CurrPtr, VerPtr; MakeV_out(FileName, V_out, &NumVert, TRUE); (*NumVertices) = NumVert; for (i=1; i<=NumVert; i++) { HeadPtr = (HeadPointer) malloc(sizeof(HeadNode)); strcpy(HeadPtr->Data, V_out[i]->Data); HeadPtr->Next = NULL; for (j=1; j<=NumVert; j++) { CurrPtr = V_out[j]->Next; while (CurrPtr!=NULL) { if (CurrPtr->Vertex==i) { VerPtr=(AdjPointer)malloc(sizeof(struct VertexNode)); VerPtr->Vertex = j; VerPtr->Length = CurrPtr->Length; VerPtr->Flow = 0; VerPtr->Next = HeadPtr->Next; HeadPtr->Next = VerPtr; } CurrPtr = CurrPtr->Next; } } V_in[i] = HeadPtr; } } void BuildGraph(char *FileName, ArrayOfEdge E, byte *NumVertices) { byte EndPt, NumVert; int i; char ch[2]; EdgePointer EdgePtr; FILE *FileData; if ((FileData = fopen(FileName, "rt")) == NULL) { 205 printf(" File khong tim thay "); return; } NumVert = 0; while (!feoln(FileData)) { ++NumVert; E[NumVert] = (ListEdgePointer) malloc(sizeof(ListEdge)); fscanf(FileData, "%s", ch); E[NumVert]->Data = ch[0]; E[NumVert]->Next = NULL; } (*NumVertices) = NumVert; for (i=1; i<=NumVert; i++) printf("%c ", E[i]->Data); printf("\n"); freadln(FileData); while (!feof(FileData)) { EdgePtr = (EdgePointer) malloc(sizeof(struct EdgeNode)); for (i=0; i<2; i++) { fscanf(FileData, "%d", &EndPt); printf("%d ", EndPt); EdgePtr->Vertex[i] = EndPt; EdgePtr->Link[i] = E[EndPt]->Next; E[EndPt]->Next = EdgePtr; } printf("\n"); freadln(FileData); } fclose(FileData); } void DFS(ArrayOfEdge E, byte Start, SetOfVertices Unvisited) { EdgePointer Ptr; byte StartEnd, OtherEnd, NewStart; SubSet(Unvisited, Start); 206 Ptr = E[Start]->Next; while ((!EmptySet(Unvisited))&&(Ptr!=NULL)) { StartEnd = 0; OtherEnd = 1; if (Ptr->Vertex[0]!=Start) { StartEnd = 1; OtherEnd = 0; } NewStart = Ptr->Vertex[OtherEnd]; if (InSet(Unvisited, NewStart)) DFS(E, NewStart, Unvisited); Ptr = Ptr->Link[StartEnd]; } } void DisplayV_out(ArrayOfPointer V_out, byte NumVertices, Boolean Weight) { int i; AdjPointer CurrPtr; for (i=1; i<=NumVertices; i++) { printf("(%d) %s ", i, V_out[i]->Data); CurrPtr = V_out[i]->Next; while (CurrPtr!=NULL) { printf("%d ", CurrPtr->Vertex); if (Weight) printf("%d ", CurrPtr->Length); CurrPtr = CurrPtr->Next; } printf("\n"); } printf("\n"); } void DisplayV_in(ArrayOfPointer V_in, byte NumVertices) { int i; AdjPointer CurrPtr; for (i=1; i<=NumVertices; i++) 207 { printf("%s ", V_in[i]->Data); CurrPtr = V_in[i]->Next; while (CurrPtr!=NULL) { printf(" %d %d", CurrPtr->Vertex, CurrPtr->Length); CurrPtr = CurrPtr->Next; } printf("\n"); } } void PathTwoVertex(Path Pred, byte Start, byte Terminal) { if (Terminal != Start) { PathTwoVertex(Pred, Start, Pred[Terminal]); printf(" > %2d", Terminal); } } void PopHeadPtr(byte NumVertices, ArrayOfPointer V_out) { byte Item; int i; AdjPointer CurrPtr; for (i=1; i<=NumVertices; i++) { CurrPtr = V_out[i]->Next; while (CurrPtr != NULL) Pop(&CurrPtr, &Item); free(V_out[i]); } } #endif 208 [...]... Soc., Vol 37, 194-197 (1941) [10] Busacker R G., Gowen P J., A procedure for determining a family of minimal-cost network flow patterns, Operations Research Office, Technical paper 15 (1961) [11] Cayley A., Collected papers, Quart Jl of Mathematics, 13 Cambridge, 26 (1897) [12] Chase S M., Analysis of algorithms for finding all spanning trees of a graph, Report No 401, Department of Computer Science, University... algorithms, John Wiley & Sons (1990) [31] Hamming R W., Coding and information theory, Prentice Hall (1980) [32] Hanan M., On Steiner’s problem with rectilinear distance, Jl of SIAM (Appl Math.), 14 255 (1966) 210 [33] Hopcroft J E., Tarjan R E., Isomorphism of planar graphs, in Complexity of Computer Computations, Plenum, New York (1972) [34] Hopcroft J E., Tarjan R E., Efficient planarity testing, J ACM 21, 549-568 . ((c=fgetc(fp))= =10) { fseek(fp, -2, 1); return(TRUE); } else if (c==EOF) return(TRUE); else { fseek(fp, -1, 1); return(FALSE); } } void freadln(FILE *fp) { char c; while (((c=fgetc(fp))! =10) &&(c!=EOF)); } void. R. L., On coloring the nodes of a network, Proc. Cambridge Phil. Soc., Vol. 37, 194-197 (1941). [10] Busacker R. G., Gowen P. J., A procedure for determining a family of minimal-cost network flow. Chase S. M., Analysis of algorithms for finding all spanning trees of a graph, Report No. 401, Department of Computer Science, University of Illinois, Urbana, Oct. (1970). [13] Chvatal V., On Hamilton’s

Ngày đăng: 25/07/2014, 23:20

TỪ KHÓA LIÊN QUAN