37
Graph C and Data Structures Baojian Hua [email protected]

Graph C and Data Structures Baojian Hua [email protected]

  • View
    243

  • Download
    3

Embed Size (px)

Citation preview

Graph

C and Data StructuresBaojian Hua

[email protected]

What’s a Graph?

Graph: a group of vertices connected by edges

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

Broad Applications

Graph Vertices Edges

communication

telephone cables

software functions calls

internet web page hyper-links

social relationship

people friendship

transportation

cities roads

… … …

Graph Terminology

1

4

2

65

3

vertex

edge:

directed vs

undirectedA sample graph taken from chapter 22 of Introduction to Algorithms.

Graph Terminology

1

4

2

65

3

degree:

in-degree vs

out-degree

Graph ADT A graph is a tuple (V, E)

V is a set of vertices: v1, v2, …, vn E is a set of edges: <vi, vj> (1<=i, j<=n)

Typical operations: graph creation search a vertex (or an edge) traverse all vertexes …

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

Representation Two popular strategies:

array-based (adjacency matrix) Keep an extensible two-dimensional array M i

nternally M[i][j] holds the edge <vi, vj>, if there exists o

ne linear list-based (adjacency list)

for every vertex vi, maintain a linear list list<vi>

list<vi> stores vi’s out-going edges

Adjacency Matrix

0

0

1

2

51 2 3

1

4

2

65

3

#

#

# #

# #

#

#

3

4

5

4

Note the mapping function:map (n) = n-1

Adjacency List

1

2

3

4

1->2

3->5 3->6

5

4->1

6

2->5

4->2

5->4

6->6

1

4

2

65

3

“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_ttypedef 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

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

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 2 3

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.

Adjacency Matrix-based: Graph CreationT 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 Verticesvoid 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 2 3 0

1

2

3

0 1 2 3

# # #

4

4

Graph Implementation #1: Inserting Edgesvoid 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 2 3 0

1

2

3

0 1 2 3

# # # #

4

4

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

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 \ 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;};

Graph Representation #2: Adjacency Liststruct 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, linkedList, data, vertex, edgeT Graph_new (){ T g = malloc (sizeof (*g)); g->vertices = LinkedList_new ();

return g;}

verticesg

/\

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

return v;} data

v

/\

edges

data

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

Adjacency List-based:Inserting New Vertexvoid Graph_insertVertex (T g, poly data)

{

Tv v = Vertex_new (data);

LinkedList_insertTail (g->vertices, v);

return;

} 0

1

2

3

0->1 0->2 0->3

4

Adjacency List-based:Inserting New Edgevoid 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->1 0->2 0->3

4

Adjacency List-based:Inserting New Edgevoid 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->1 0->2 0->3

4

Adjacency List-based:Inserting New Edgevoid 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->1 0->2 0->3

4

Adjacency List-based:Inserting New Edgevoid 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->1 0->2 0->3

4

0->4

Adjacency List-based:Inserting New Edgevoid 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->1 0->2 0->3

4

0->4

Example

1

4

2

65

3

Client Code for This Example:Step #1: Cook DataT 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

Client Code Continued:Step #2: Insert VerticesGraph_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

311

4

2

65

3

Client Code Continued:Step #3: Insert EdgesGraph_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

All in Picture:An Empty Graph// I’ll make use of this convention for colors:

// graph, linkedList, data, vertex, edge

nextdata

g

All in Picture:After Inserting all Vertices// I’ll make use of this convention for colors:

// graph, linkedList, data, vertex, edge

nextdata

g

/\

data

next /\ /\ /\ /\ /\

1 2 3 64 5

All in Picture:After Inserting all Edges (Part)// I’ll make use of this convention for colors:

// graph, linkedList, data, vertex, edge

nextdata

g

/\

data

next /\ /\ /\ /\

1 2 3 64 5

/\ /\

fromto

fromto