82
8 8 第第第第第第第第Graphs Graphs 8-1 8-1 圖圖圖圖圖圖圖 圖圖圖圖圖圖圖 8-2 8-2 圖圖圖圖圖圖 圖圖圖圖圖圖 8-3 8-3 圖圖圖圖圖 圖圖圖圖圖 8-4 8-4 圖圖圖 圖圖圖 8-5 8-5 圖圖圖圖圖 圖圖圖圖圖 - - 圖圖圖圖 圖圖圖圖 8-6 8-6 圖圖圖圖圖 圖圖圖圖圖 - - 圖圖圖圖 圖圖圖圖

第 8 章 圖形結構(Graphs)

  • Upload
    goldy

  • View
    92

  • Download
    0

Embed Size (px)

DESCRIPTION

第 8 章 圖形結構(Graphs). 8-1 圖形的基本觀念 8-2 圖形的表示法 8-3 圖形的走訪 8-4 擴張樹 8-5 圖形的應用 - 最短路徑 8-6 圖形的應用 - 拓樸排序. 8-1 圖形的基本觀念 - 說明. 在日常生活中,我們常常將複雜觀念或問題使用圖形來表達,例如:在進行系統分析、電路分析、電話佈線和企劃分析等。因為圖形化可以讓人更容易了解,所以「圖形」( Graph )是資料結構一種十分重要的結構。例如:城市之間的公路圖,如下圖所示:. 8-1 圖形的基本觀念 - 定義. - PowerPoint PPT Presentation

Citation preview

  • 8 Graphs8-1 8-2 8-3 8-4 8-5 - 8-6 -

  • 8-1 -Graph

  • 8-1 - 8.1GVEG = ( V, E )VVerticesEEdges

  • 8-1 -G1G2

  • 8-1 -5V1V2V5V(G1)G1V(G2)G2V(G1) = { V1, V2, V3, V4, V5 }V(G2) = { V1, V2, V3, V4, V5 }G16G24E(G1)G1E(G2)G2E(G1) = { (V1,V2),(V1,V3),(V2,V3),(V2,V4),(V3,V5),(V4,V5) }E(G2) = { (V1,V2),(V1,V3),(V2,V4),(V2,V5) }(V1,V2)V1V2

  • 8-1 -E(G)Undirected GraphG1G2(V1,V2)(V2,V1)Directed Graph

  • 8-1 -G3G3V(G3)E(G3)V(G3)={ V1, V2, V3, V4, V5 }E(G3)={ ,,,,,, }

  • 8-1 -1Complete Graphnn(n-1)/24G46

    SubgraphGGGGGGG5G6G4

  • 8-1 -2Connected GraphG1G2G3G4G5G6Disconnected GraphG73

  • 8-1 -3Simple Directed Path11G75,2,15,2,1,4,2,1Cycle11G75,2,4,5115

  • 8-1 -4AdjacentIn-degreeG71122Out-degreeG7151

  • 8-2 8-2-1 8-2-2

  • 8-2 Adjacency MatrixAdjacency Lists

  • 8-2-1 -G = (V, E)nn X nCn X n01

  • 8-2-1 -G15 X 5V1V2(V1,V2)(V2,V1)1V1V2V2V1

  • 8-2-1 -01: /* : Ch8-2-1.h */02: #define MAX_VERTICES 6 /* */03: int graph[MAX_VERTICES][MAX_VERTICES];04: /* */05: extern void createGraph(int len, int *edge);06: extern void printGraph();

  • void createGraph(int len, int *edge) { int from, to; /* */ int i, j; for ( i = 1; i < MAX_VERTICES; i++ ) for ( j = 1; j < MAX_VERTICES; j++ ) if ( i != j ) graph[i][j] = MAX; /* */ else graph[i][j] = 0; /* */ for ( i = 0; i < len; i++ ) { /* */ from = edge[i*3]; /* */ to = edge[i*3+1]; /* */ graph[from][to]=edge[i*3+2];/* */ }}/* : */void printGraph() { int i, j; /* */ for ( i = 1; i < MAX_VERTICES; i++ ) { for ( j = 1; j < MAX_VERTICES; j++ ) if ( graph[i][j] == MAX ) printf(" "); else printf("%4d", graph[i][j]); printf("\n"); }}

  • 8-2-2 -G1

  • 8-2-2 -01: /* : Ch8-2-2.h */02: #define MAX_VERTICES 10 /* */03: struct Vertex { /* */04: int data; /* */05: struct Vertex *next; /* */06: };07: typedef struct Vertex *Graph; /* */08: struct Vertex head[MAX_VERTICES];09: /* */10: extern void createGraph(int len, int *edge);11: extern void printGraph();12: extern void dfs(int vertex);13: extern void bfs(int vertex);

  • void createGraph(int len, int *edge) { Graph newnode, ptr; /* */ int from, to; /* */ int i; for ( i = 1; i < MAX_VERTICES; i++ ) { head[i].data = i; /* */ head[i].next = NULL; /* */ } for ( i = 0; i < len; i++ ) { /* */ from = edge[i*2]; /* */ to = edge[i*2+1]; /* */ /* */ newnode = (Graph)malloc(sizeof(struct Vertex)); newnode->data = to; /* */ newnode->next = NULL; /* */ ptr = &(head[from]); /* */ while ( ptr->next != NULL ) /* */ ptr = ptr->next; /* */ ptr->next = newnode; /* */ }}/* : */void printGraph() { Graph ptr; int i; /* */ for ( i = 1; i < MAX_VERTICES; i++ ) { ptr = head[i].next; /* */ if ( ptr != NULL ) { /* */ printf("V%d =>", head[i].data);/* */ while ( ptr != NULL ) { /* */ printf("V%d ", ptr->data); /* */ ptr = ptr->next; /* */ } printf("\n"); } }

  • 8-2-2 -1createGraph()forhead[]nextNULL(1, 2)122newnode

  • 8-2-2 -2head[]11

  • 8-2-2 -3(2, 1)21head[]2

    G1

  • 8-2-2 -printGraph()printGraph()head[]whilewhile ( ptr != NULL ) { printf(V%d , ptr->data); //show ptr = ptr->next; //}

  • 8-3 8-3-1 DFS8-3-2 BFS

  • 8-3 -1G8

  • 8-3 -2G8

  • 8-3 -G8Depth-first Search, DFS Breadth-first Search, BFS

  • 8-3-1 DFS-G81112224488855288663712485637

  • 8-3-1 DFS-dfs(V)Step 1V1visited[vertex] = 1;Step 2VWdfs(W)while ( ptr != NULL ) { if ( visited[ptr->data] == 0 ) // dfs(ptr->data); ptr = ptr->next;}

  • #include #include #include "ex8.h"#include "createGraph.c"int visited[MAX_VERTICES]; /* *//* : */ void dfs(int vertex, int pre) { Graph ptr; visited[vertex] = 1; /* */ if ( pre != 0 ) printf("[V%d]->[V%d] ", pre, vertex); ptr = head[vertex].next; /* */ pre = vertex; while ( ptr != NULL ) { /* */ if ( visited[ptr->data] == 0 ) /* */ dfs(ptr->data, pre); /* */ ptr = ptr->next; /* */ }}/* */int main() { int edge[20][2] = { {1, 2}, {2, 1}, /* */ {1, 3}, {3, 1}, {2, 4}, {4, 2}, {2, 5}, {5, 2}, {3, 6}, {6, 3}, {3, 7}, {7, 3}, {4, 8}, {8, 4}, {5, 8}, {8, 5}, {6, 8}, {8, 6}, {7, 8}, {8, 7} }; int i; /* */ for ( i = 1; i < MAX_VERTICES; i++ ) visited[i] = 0; createGraph(20, &edge[0][0]); /* */ printf("G:\n"); printGraph(); /* */ printf(":\n"); dfs(1, 0); /* */ printf("\n"); system("PAUSE"); return 0; }

  • 8-3-2 BFS-G8112323245367812345678

  • 8-3-2 BFS-1bfs(V)Step 1V1visited[vertex] = 1;Step 2Venqueue(vertex);

  • 8-3-2 BFS-2Step 3while ( !isQueueEmpty() ) {(1) V vertice = dequeue(); ptr = head[vertex].next; //linklist(2) VW while ( ptr != NULL ) { if ( visited[ptr->data]==0 ) { enqueue(ptr->data); visited[ptr->data] = 1; printf("[V%d] ", ptr->data); } ptr = ptr->next; }}

  • 8-4 8-4-1 8-4-2 8-4-3

  • 8-4 -Spanning Trees1

  • 8-4 -

  • 8-4-1 -8-3DFS Spanning TreesBFS Spanning Trees

  • 8-4-1 -G81,2,4,8,5,6,3,7

  • 8-4-1 -G81,2,3,4,5,6,7,812,324,536,748

  • 8-4-2 -Weights

  • 8-4-2 -G9

  • 8-4-2 -10G9

  • 8-4-2 -weightG9

  • 8-4-3 -Minimum-cost Spanning Trees

  • 8-4-3 -G10

  • 8-4-3 -Kruskal

  • 8-4-3 -1Step 1(1, 2)

  • 8-4-3 -2,3Step 2(2, 4)(2, 4)Step 3(1, 4)(3, 5)

  • 8-4-3 -4Step 4(2, 5)(2, 5)

  • 8-4-3 -

  • 8-4-3 -01: /* : Ch8-4-3.h */02: #define MAX_VERTICES 6 /* */03: struct Edge { /* */04: int from; /* */05: int to; /* */06: int weight; /* */07: struct Edge *next; /* */08: };09: typedef struct Edge *EdgeList; /* */10: EdgeList first = NULL; /* */11: int vertex[MAX_VERTICES]; /* */12: /* */13: extern void createEdge(int len, int *edge);14: extern void minSpanTree();15: extern void addSet(int from, int to);16: extern int isSameSet(int from, int to);

  • 8-4-3 -minSpanTree()Step 1(1) if ( !isSameSet(ptr->from,ptr->to) ) addSet(ptr->from,ptr->to);(2) ptr = ptr->next;

  • 8-5 - 8-5-1 8-5-2

  • 8-5 - Shortest Paths ProblemsG = (V, E)SourceDijkstraFloyd

  • 8-5-1 -Dijkstra

  • 8-5-1 -1

  • 8-5-1 -DijkstraDijkstraSingle-source Shortest PathsDijkstraG11

  • 8-5-1 -DijkstraDijkstraStep 1(1) graph[source][i]dist[i](2) selected[i]pi[i]Step 21(1) dist[]W(2) Wselected[W] = 1(3) dist[]X1) dist[X]graph[W][X]+dist[W]a. graph[W][X]+dist[W]dist[X]b. XWpi[X] = W

  • 8-5-1 -DijkstraDijkstraG111dist[ ]:pi[ ]:

  • 8-5-1 -dist[]W235dist[]X3456MIN (dist(X),dist(W)+(W,X))MIN()dist(W)+(W,X)13dist[3] = 9023dist(2) + (2, 3) = dist[2] + graph[2][3] = 35 + 45 = 8080dist[3] = 90dist[3] = 80pi[3]232465//Pi[ N]?

  • 8-5-1 -2dist[]W465dist[]X35654dist[4] + graph[4, 5] = 65 + 45 = 110dist[5] = 110pi[5]4541

  • 8-5-1 -pi[]16pi[6]5pi[5] = 353321,2,3,5,6

  • 8-5-2 -R. W. FloydG11Floyddist[][]

  • 8-5-2 -Floydn(i, j)distn(i,j)=MIN(distn-1(i,j),distn-1(i,n)+distn-1(n,j))nnMIN()distn-1distn(i,j)ndistn-1(i,j)ndistn-1(i,n)+distn-1(n,j)distn-1(i,j)ninnj

  • 8-5-2 -dist1(i,j) = MIN(dist0(i,j),dist0(i,1)+dist0(1,j))dist0

  • 8-5-2 -dist2(i,j) = MIN(dist1(i,j),dist1(i,2)+dist1(2,j))

  • 8-5-2 -6

  • #include #include #include "adjacencyMatrix.c"int dist[MAX_VERTICES]; /* */int pi[MAX_VERTICES]; /* *//* : */ void shortestPath(int source, int num) { int selected[MAX_VERTICES]; /* */ int min_len; /* */ int min_vertex = 1; /* */ int i,j; for ( i = 1; i
  • int main() { int edge[7][3] = { {1, 2, 35}, /* */ {1, 3, 90}, {2, 3, 45}, {2, 4, 30}, {3, 5, 25}, {4, 5, 45}, {5, 6, 200} }; int end = 6; createGraph(7, &edge[0][0]); /* */ printf("G:\n"); printGraph(); /* */ printf("Dijkstra:\n"); shortestPath(1,6); /* */ printf("\n(): "); while ( end != 0 ) { printf("%4d", end); /* */ end = pi[end]; } printf("\n"); printf("100: "); for ( end = 2; end
  • 8-6 - (AOV)Activity On Vertex NetworkAOVCCTopological Sort

  • 8-6 - (AOV1)G12

  • 8-6 - (AOV2)G12AOV1323Predecessor2156Immediate Predecessor41,5,7Successor157Immediate Successor

  • 8-6 - (1)00

  • 8-6 - (2)2022

  • 8-6 - (3)1560156

  • 8-6 - ()G12

  • 8-6 - (4)7743215674

  • 8-6 - ()Step 1head[]0for ( i = 1; i data;head[vertex].data--;(3) 10if ( head[vertex].data == 0 ) enqueue(vertex); ptr = ptr->next;}}

  • 8-6 - (AOV1)AOVAOVG122

  • 8-6 - (AOV2)5675321657657