Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph C and Data Structures Baojian Hua

Similar presentations


Presentation on theme: "Graph C and Data Structures Baojian Hua"— Presentation transcript:

1 Graph C and Data Structures Baojian Hua bjhua@ustc.edu.cn

2 What ’ s a Graph? Graph: a group of vertices connected by edges

3 Why Study Graphs? Interesting & broadly used abstraction not only in computer science challenge branch in discrete math Ex: the 4-color problem hundreds of known algorithms with more to study numerous applications

4 Broad Applications GraphVerticesEdges communicationtelephonecables softwarefunctionscalls internetweb pagehyper-links social relationship peoplefriendship transportationcitiesroads ………

5 Graph Terminology 1 4 2 65 3 vertex edge: directed vs undirected A sample graph taken from chapter 22 of Introduction to Algorithms.

6 Graph Terminology 1 4 2 65 3 degree: in-degree vs out-degree

7 Graph ADT A graph is a tuple (V, E) V is a set of vertices: v1, v2, …, vn E is a set of edges: (1<=i, j<=n) Typical operations: graph creation search a vertex (or an edge) traverse all vertexes …

8 Example G = (V, E) V = {1, 2, 3, 4, 5, 6} E = {(1, 2), (2, 5), (3, 5), (3, 6), (4, 1), (4, 2), (5, 4), (6, 6)} 1 4 2 65 3

9 Representation Two popular strategies: array-based (adjacency matrix) Keep an extensible two-dimensional array M internally M[i][j] holds the edge, if there exists one linear list-based (adjacency list) for every vertex vi, maintain a linear list list list stores vi ’ s out-going edges

10 Adjacency Matrix 0 0 1 2 5123 1 4 2 65 3 # # ## ## # # 3 4 5 4 Note the mapping function: map (n) = n-1

11 Adjacency List 1 2 3 4 1->2 3->53->6 5 4->1 6 2->5 4->2 5->4 6->6 1 4 2 65 3

12 “ graph ” ADT: Interface // This slides assume all graphs directed. // Undirected ones are similar and easier. // In file “graph.h” #ifndef GRAPH_H #define GRAPH_H #define T Graph_t typedef struct T *T; T Graph_new (); void Graph_insertVertex (T g, poly data); void Graph_insertEdge (T g, poly from, poly to); // more to come later #undef T #endif

13 Client Code graph g = newGraph (); insertVertex (g, 1); insertVertex (g, 2); … insertVertex (g, 6); insertEdge (g, 1, 2); insertEdge (g, 2, 5); … insertEdge (g, 6, 6); 1 4 2 65 3

14 Graph Implementation #1: Adjacency Matrix // adjacency matrix-based implementation #include “matrix.h” #include “dict.h” #include “graph.h” struct Graph_t { Matrix_t m; // remember the index Dict_t d; }; ### 0 1 2 3 0 1 23

15 Matrix Interface // file “matrix.h” #ifndef MATRIX_H #define MATRIX_H #define T Matrix_t typedef struct T *T; T Matrix_new (); void Matrix_assign (matrix m, int i, int j); int Matrix_getNextIndex (matrix m); #endif // Implementation may make use of a two- // dimensional extensible array, leave to you.

16 Adjacency Matrix-based: Graph Creation T Graph_new () { T g = malloc (sizeof (*g)); g->m = Matrix_new (); // an empty matrix g->d = Dict_new (); // an empty dictionary return g; }

17 Adjacency Matrix-based: Inserting Vertices void Graph_insertVertex (T g, poly data) { int i = Matrix_getNextIndex (g->matrix); Dict_insert (g->dict, data, i); return; } ### 0 1 2 3 0 1 230 1 2 3 0123 ### 4 4

18 Graph Implementation #1: Inserting Edges void Graph_insertEdge (T g, poly from, poly to) { int f = Dict_Lookup (g->dict, from); int t = Dict_Lookup (g->dict, to); Matrix_assign (g->matrix, f, t); return; } ### 0 1 2 3 0 1 230 1 2 3 0123 #### 4 4

19 Summary Pros: Relatively easy to implement But need another level of indirection from data to array indexes Searching vertices or edges can be fast May be too space-consuming many empty slots in matrix it the graph has many vertices but few edges

20 Graph Representation #2: Adjacency List #include “linked-list.h” #include “graph.h” #define T Graph_t #define Tv Vertex_t #define Te Edge_t #define List_Tv \ LinkedList_t #define List_Te \ LinkedList_t typedef struct Tv *Tv; typedef struct Te *Te; struct T { List_Tv vertices; }; struct Tv { poly data; List_Te edges; }; struct Te { Tv from; Tv to; };

21 Graph Representation #2: Adjacency List struct T { List_Tv vertices; }; struct Tv { poly data; List_Te edges; }; struct Te { Tv from; Tv to; } 0 1 2 3 0->10->20->3

22 Adjacency List-based: Graph Creation // Convention for colors: // graph, linkedList, data, vertex, edge T Graph_new () { T g = malloc (sizeof (*g)); g->vertices = LinkedList_new (); return g; } vertices g /\

23 Adjacency List-based: Creating New Vertex // Convention for colors: // graph, linkedList, data, vertex, edge Tv Vertex_new (poly data) { Tv v = malloc (sizeof (*v)); v->data = data; v->edges = LinkedList_new (); return v; } data v /\ edges data

24 Adjacency List-based: Creating New Edge // Convention for colors: // graph, linkedList, data, vertex, edge Te Edge_new (Tv from, Tv to) { Te e = malloc (sizeof (*e)); e->from = from; e->to = to; return e; } from e to from to

25 Adjacency List-based: Inserting New Vertex void Graph_insertVertex (T g, poly data) { Tv v = Vertex_new (data); LinkedList_insertTail (g->vertices, v); return; } 0 1 2 3 0->10->20->3 4

26 Adjacency List-based: Inserting New Edge void Graph_insertEdge (T g, poly from, poly to) { Tv vf = lookupVertex (g, from); Tv vt = lookupVertex (g, to); Te e = Edge_new (vf, vt); LinkedList_insertTail (vf->edges, e); return; } // insert 0->4 0 1 2 3 0->10->20->3 4

27 Adjacency List-based: Inserting New Edge void Graph_insertEdge (T g, poly from, poly to) { Tv vf = lookupVertex (g, from); Tv vt = lookupVertex (g, to); Te e = Edge_new (vf, vt); LinkedList_insertTail (vf->edges, e); return; } 0 1 2 3 0->10->20->3 4

28 Adjacency List-based: Inserting New Edge void Graph_insertEdge (T g, poly from, poly to) { Tv vf = lookupVertex (g, from); Tv vt = lookupVertex (g, to); Te e = Edge_new (vf, vt); LinkedList_insertTail (vf->edges, e); return; } 0 1 2 3 0->10->20->3 4

29 Adjacency List-based: Inserting New Edge void insertEdge (graph g, poly from, poly to) { Tv vf = lookupVertex (g, from); Tv vt = lookupVertex (g, to); Te e = Edge_new (vf, vt); LinkedList_insertTail (vf->edges, e); return; } 0 1 2 3 0->10->20->3 4 0->4

30 Adjacency List-based: Inserting New Edge void insertEdge (T g, poly from, poly to) { Tv vf = lookupVertex (g, from); Tv vt = lookupVertex (g, to); Te e = Edge_new (vf, vt); LinkedList_insertTail (vf->edges, e); return; } 0 1 2 3 0->10->20->3 4 0->4

31 Example 1 4 2 65 3

32 Client Code for This Example: Step #1: Cook Data T g = Graph_new (); Int_t n1 = Int_new (1); Int_t n2 = Int_new (2); Int_t n3 = Int_new (3); Int_t n4 = Int_new (4); Int_t n5 = Int_new (5); Int_t n6 = Int_new (6); 4 2 65 31

33 Client Code Continued: Step #2: Insert Vertices Graph_insertVertex (g, n1); Graph_insertVertex (g, n2); Graph_insertVertex (g, n3); Graph_insertVertex (g, n4); Graph_insertVertex (g, n5); Graph_insertVertex (g, n6); 4 2 65 31 1 4 2 65 3

34 Client Code Continued: Step #3: Insert Edges Graph_insertEdge (g, n1, n2); Graph_insertEdge (g, n2, n5); Graph_insertEdge (g, n3, n5); Graph_insertEdge (g, n3, n6); Graph_insertEdge (g, n4, n1); Graph_insertEdge (g, n4, n2); Graph_insertEdge (g, n5, n4); Graph_insertEdge (g, n6, n6); // Done! :-) 1 4 2 65 3

35 All in Picture: An Empty Graph // I’ll make use of this convention for colors: // graph, linkedList, data, vertex, edge next data g

36 All in Picture: After Inserting all Vertices // I’ll make use of this convention for colors: // graph, linkedList, data, vertex, edge next data g /\ data next/\ 1 23 6 4 5

37 All in Picture: After Inserting all Edges (Part) // I’ll make use of this convention for colors: // graph, linkedList, data, vertex, edge next data g /\ data next/\ 1 23 6 4 5 from to from to


Download ppt "Graph C and Data Structures Baojian Hua"

Similar presentations


Ads by Google