Graph C and Data Structures Baojian Hua bjhuaustc
Graph C and Data Structures Baojian Hua bjhua@ustc. edu. cn
What’s a Graph? n Graph: a group of vertices connected by edges
Why Study Graphs? n Interesting & broadly used abstraction n n challenge branch in discrete math n n Ex: the 4 -color problem hundreds of known algorithms n n not only in computer science with more to study numerous applications
Broad Applications Graph Vertices Edges communication software internet social relationship transportation … telephone functions web page people cables calls hyper-links friendship cities … roads …
Graph Terminology 1 2 3 4 5 6 vertex edge: directed vs undirected A sample graph taken from chapter 22 of Introduction to Algorithms.
Graph Terminology 1 2 3 4 5 6 degree: in-degree vs out-degree
Graph ADT n A graph is a tuple (V, E) n n n V is a set of vertices: v 1, v 2, …, vn E is a set of edges: <vi, vj> (1<=i, j<=n) Typical operations: n n graph creation search a vertex (or an edge) traverse all vertexes …
Example G = (V, E) V = {1, 2, 3, 1 2 3 4 5 6 4, 5, 6} E = {(1, 2), (2, 5), (3, 6), (4, 1), (4, 2), (5, 4), (6, 6)}
Representation n Two popular strategies: n array-based (adjacency matrix) n n n Keep an extensible two-dimensional array M internally M[i][j] holds the edge <vi, vj>, if there exists one linear list-based (adjacency list) n n for every vertex vi, maintain a linear list<vi> stores vi’s out-going edges
Adjacency Matrix 0 1 0 2 3 4 # # # 2 3 4 5 6 5 # 1 1 # # Note the mapping function: map (n) = n-1
Adjacency List 1 1 ->2 2 2 ->5 3 3 ->5 3 ->6 4 4 ->1 4 ->2 5 5 ->4 6 6 ->6 1 2 3 4 5 6
“graph” ADT: Interface // This slides assume all graphs directed. // Undirected ones are similar and easier. // In file “graph. h” #ifndef GRAPH_H #define T Graph_t typedef struct T *T; T Graph_new (); void Graph_insert. Vertex (T g, poly data); void Graph_insert. Edge (T g, poly from, poly to); // more to come later #undef T #endif
Client Code graph g = new. Graph (); insert. Vertex (g, 1); 1 2 3 4 5 6 insert. Vertex (g, 2); … insert. Vertex (g, 6); insert. Edge (g, 1, 2); insert. Edge (g, 2, 5); … insert. Edge (g, 6, 6);
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 0 1 2 3 # # #
Matrix Interface // file “matrix. h” #ifndef MATRIX_H #define T Matrix_t typedef struct T *T; T Matrix_new (); void Matrix_assign (matrix m, int i, int j); int Matrix_get. Next. Index (matrix m); #endif // Implementation may make use of a two// dimensional extensible array, leave to you.
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; }
Adjacency Matrix-based: Inserting Vertices void Graph_insert. Vertex (T g, poly data) { int i = Matrix_get. Next. Index (g->matrix); Dict_insert (g->dict, data, i); return; 0 1 2 3 } 0 2 3 0 1 # # # 0 1 2 3 # # # 1 2 3 4 4
Graph Implementation #1: Inserting Edges void Graph_insert. Edge (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); 0 1 2 3 4 return; } 0 2 3 0 1 # # 0 1 2 3 # # # 1 2 3 4
Summary n Pros: n Relatively easy to implement n n n But need another level of indirection from data to array indexes Searching vertices or edges can be fast May be too space-consuming n many empty slots in matrix it the graph has many vertices but few edges
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 Linked. List_t #define List_Te Linked. List_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; };
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 ->1 0 ->2 0 ->3
Adjacency List-based: Graph Creation // Convention for colors: // graph, linked. List, data, vertex, edge T Graph_new () { T g = malloc (sizeof (*g)); g->vertices = Linked. List_new (); return g; } g vertices /
Adjacency List-based: Creating New Vertex // Convention for colors: // graph, linked. List, data, vertex, edge Tv Vertex_new (poly data) { Tv v = malloc (sizeof (*v)); v->data = data; v->edges = Linked. List_new (); return v; } v data edges /
Adjacency List-based: Creating New Edge // Convention for colors: // graph, linked. List, data, vertex, edge Te Edge_new (Tv from, Tv to) { Te e = malloc (sizeof (*e)); e->from = from; e->to = to; return e; } e from to to
Adjacency List-based: Inserting New Vertex void Graph_insert. Vertex (T g, poly data) { Tv v = Vertex_new (data); Linked. List_insert. Tail (g->vertices, v); return; 0 ->1 0 ->2 0 } 1 2 3 4 0 ->3
Adjacency List-based: Inserting New Edge void Graph_insert. Edge (T g, poly from, poly to) { Tv vf = lookup. Vertex (g, from); Tv vt = lookup. Vertex (g, to); Te e = Edge_new (vf, vt); 0 ->1 0 ->2 0 ->3 0 Linked. List_insert. Tail (vf->edges, e); return; 1 } // insert 0 ->4 2 3 4
Adjacency List-based: Inserting New Edge void Graph_insert. Edge (T g, poly from, poly to) { Tv vf = lookup. Vertex (g, from); Tv vt = lookup. Vertex (g, to); Te e = Edge_new (vf, vt); 0 ->1 0 ->2 0 ->3 0 Linked. List_insert. Tail (vf->edges, e); return; 1 } 2 3 4
Adjacency List-based: Inserting New Edge void Graph_insert. Edge (T g, poly from, poly to) { Tv vf = lookup. Vertex (g, from); Tv vt = lookup. Vertex (g, to); Te e = Edge_new (vf, vt); 0 ->1 0 ->2 0 ->3 0 Linked. List_insert. Tail (vf->edges, e); return; 1 } 2 3 4
Adjacency List-based: Inserting New Edge void insert. Edge (graph g, poly from, poly to) { Tv vf = lookup. Vertex (g, from); Tv vt = lookup. Vertex (g, to); Te e = Edge_new (vf, vt); 0 ->1 0 ->2 0 ->3 0 Linked. List_insert. Tail (vf->edges, e); return; 1 } 2 3 4 0 ->4
Adjacency List-based: Inserting New Edge void insert. Edge (T g, poly from, poly to) { Tv vf = lookup. Vertex (g, from); Tv vt = lookup. Vertex (g, to); Te e = Edge_new (vf, vt); 0 ->1 0 ->2 0 ->3 0 Linked. List_insert. Tail (vf->edges, e); return; 1 } 2 3 4 0 ->4
Example 1 2 3 4 5 6
Client Code for This Example: Step #1: Cook Data T g = Graph_new (); Int_t Int_t n 1 n 2 n 3 n 4 n 5 n 6 = = = Int_new Int_new (1); (2); (3); (4); (5); (6); 1 2 3 4 5 6
Client Code Continued: Step #2: Insert Vertices Graph_insert. Vertex (g, (g, (g, n 1); n 2); n 3); n 4); n 5); n 6); 1 4 2 5 3 6
Client Code Continued: Step #3: Insert Edges Graph_insert. Edge Graph_insert. Edge (g, (g, n 1, n 2, n 3, n 4, n 5, n 6, n 2); n 5); n 6); n 1); n 2); n 4); n 6); 1 2 3 4 5 6 // Done! : -)
All in Picture: An Empty Graph g // I’ll make use of this convention for colors: // graph, linked. List, data, vertex, edge next data
All in Picture: After Inserting all Vertices g // I’ll make use of this convention for colors: // graph, linked. List, data, vertex, edge next data 1 / 2 3 5 4 6 data next / / /
All in Picture: After Inserting all Edges (Part) g // I’ll make use of this convention for colors: // graph, linked. List, data, vertex, edge next data 1 / 2 3 5 4 6 data next from to / /
- Slides: 37