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 node edge: directed vs undirected

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

7 Graph ADT A graph is a tuple (V, E) where V is a set of vertices v1, v2, … and E is a set of vertex tuple Typical operations: graph creation search a vertex (or an edge) traverse all graph vertexes …

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

9 “ graph ” ADT in C: Interface // We assume, in this slides, all graphs directed, // and undirected ones are similar and easier. // in file “graph.h” #ifndef GRAPH_H #define GRAPH_H typedef struct graph *graph; graph new (); void insertVertex (graph g, poly x); void insertEdge (graph g, poly from, poly to); // we’d see more later … #endif

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

11 Implementation We ’ d study two impl ’ strategies: array-based for vertex v0, v1, …, vn, keep a two dimension array G[n+1][n+1] G[i][j] holds the edge, if there exists one linear list-based for every vertex vi, maintain a linear list l l stores vi ’ s adjacent vertices

12 Implementation#1: Adjacency Matrices // A simplified version (no edge info): #define initSize 4 struct graph { int (*matrix)[initSize][initSize]; int maxSize; int curSize; }; 0 23 0 1 3 1 2 matrix maxSize g curSize

13 graph new () { graph g = checkedMalloc (sizeof (*g)); g->matrix = checkedMalloc (sizeof (g->matrix)); for (int i=0; i<initSize; i++) for (int j=0; j<initSize; j++) (g->matrix)[i][j] = 0; g->maxSize = initSize; g->curSize = -1; return g; }

14 new () 0 23 0 1 3 1 2 matrix maxSize g curSize=-1

15 insertVertex (graph g, poly x) void insertVertex (graph g, poly x) { // assign each vertex x a number (array index) // maintain a dictionary internally int index = assignIndex (x); if (index >= g->maxSize) { // extend the array, just like the extArray extendArray (g->matrix); g->maxSize = …; //depends on extension factor } g->curSize ++; return; }

16 insertVertex on This Graph 1 4 2 65 3 graph g = newGraph (); insertVertex (g, 1); insertVertex (g, 2); … // suppose we have this mapping: 1==>0; 2==>1; …

17 insertVertex 0 23 0 1 3 1 2 matrix maxSize g curSize=4 After inserting vertex 1, 2, 3, 4; the matrix is full, so we ’ d extend it. Suppose the extension factor is 2.

18 insertVertex 0 23 0 1 3 1 2 matrix maxSize g curSize=6 4567 4 5 6 7

19 insertEdge (graph g, poly from, poly to) void insertEdge (graph g, poly from, poly to) { // assign vertex from, to numbers (array index) // maintain internal a mapping int indexFrom = assignIndex (from); int indexTo = assignIndex (to); (g->matrix)[indexFrom][indexTo] = 1; return; }

20 insertVertex on This Graph 1 4 2 65 3 graph g = newGraph (); insertEdge (g, 1, 2); insertEdge (g, 2, 5); …

21 insertVertex 0 23 0 1 3 1 2 matrix maxSize g curSize=6 1 1 11 11 1 1 4567 4 5 6 7

22 Moral We should keep a dictionary (symbol table) to record and assign array index to graph vertex any representation strategy should do the dictionary should be kept in the graph Information in the matrix is too coarse we may want to associate some info (say weight) with graph edges Need a more fancy version of array slots

23 More Fancy Edge Definition // define a more fancy edge: typedef struct edge *edge; struct edge { int from; int to; poly info; };

24 Adjacency Matrices // A simplified version: #define initSize 4 struct graph { edge (*matrix)[initSize][initSize]; int maxSize; int curSize; }; 0 23 0 1 3 1 2 matrix maxSize g curSize

25 insertEdge (graph g, poly from, poly to) void insertEdge (graph g, poly from, poly to, poly edgeInfo) { // assign vertex from, to numbers (array index) int indexFrom = assign (from); int indexTo = assign (to); edge e = newEdge (indexFrom, indexTo, edgeInfo); (g->matrix)[indexFrom][indexTo] = e; return; }

26 Implementation#2: Adjacency Lists // in file “alGraph.c” struct graph{ poly info; struct succ *succs; struct graph *next; }; struct succ{ struct graph *v; struct succ *next; }; info succs next info succs next v v v /\

27 Or With Edges struct graph{ poly info; struct edge *edges; struct graph *next; }; struct edge { struct vertex *from; struct vertex *to; struct edge *next; }; info edges next from next to /\

28 Use of Linear List // syntax T is not supported by C, which is // put here for illustrative purpose struct graph{ list vertices; }; struct vertex{ poly info; list edges; }; struct edge{ struct vertex *from; struct vertex *to; } info edges next from next to /\

29 graph new () { graph g = checkedMalloc (sizeof (*g)); g->vertices = newLinkedList (); return g; } /\ g

30 insertVertex (graph g, poly x) void insertVertex (graph g, poly x) { vertex v = newVertex (x); linkedListInsertHead (g->vertices, v); return; } // with void newVertex (poly x){ vertex v = checkedMalloc (sizeof (*v)); v->info = x; v->edges = newLinkedList (); return v; }

31 insertVertex on This Graph 1 4 2 65 3 graph g = newGraph (); insertVertex (g, 1); insertVertex (g, 2); …

32 Example of InsertVertex next data g /\ 123456

33 insertEdge (graph g, poly from, poly to) void insertEdge (graph g, poly from, poly to) { vertex f = searchVertex (g->vertices, from); vertex t = searchVertex (g->vertices, to); edge e = newEdge (f, t); linkedListInsertHead (f->edges, e); return; }

34 insertVertex on This Graph 1 4 2 65 3 graph g = newGraph (); insertEdge (g, 1, 2); insertVertex (g, 2, 5); …

35 Example of InsertEdge next data g /\ 123456 from to from to


Download ppt "Graph C and Data Structures Baojian Hua"

Similar presentations


Ads by Google