52
Graph Traversal C and Data Structures Baojian Hua [email protected]

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

  • View
    236

  • Download
    3

Embed Size (px)

Citation preview

Graph Traversal

C and Data StructuresBaojian Hua

[email protected]

Traversal A systematic way to visit vertices in a

graph Two general approaches:

breath first searching (BFS) start from one vertex, first visit all the adjacency

vertices depth first searching (DFS)

eager method

These slides assume the adjacency list representation

“graph” ADT in C: Interface// in file “graph.h”#ifndef GRAPH_H#define GRAPH_H#define T Graph_ttypedef struct T *T;typedef void (*tyVisit)(poly);

T Graph_new ();void Graph_insertVertex (T g, poly data);void Graph_insertEdge (T g, poly from, poly to);void Graph_dfs (T g, poly start, tyVisit visit);void Graph_bfs (T g, poly start, tyVisit visit);// we’d see more later…#endif

Sample Graph

1

4

2

65

3

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

print 1;

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

print 1;

// a choice

print 2;

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 4;

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 4;

print 5;

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 4;

print 5;

// a choice

print 3;

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 4;

print 5;

// a choice

print 3;

print 6;

BFS Algorithmvoid bfsOneVertex (Tv start, tyVisit visit){ Queue_t q = Queue_new ();

Queue_en (q, start); while (q not empty) { Tv current = Queue_de (q); visit (current); for (each adjacent vertex u of “current”) if (not visited u) Queue_en (q, u); }}

BFS Algorithmvoid bfs (T g, poly start, tyVisit visit){ Tv startV = searchVertex (g, start); bfsOneVertex (startV, visit);

for (each vertex u in graph g) if (not visited u) bfsOneVertex (q, u);}

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_output);

Queue: 1

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_output);

print 1;

Queue: 2, 4

Queue: 1

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

print 1;

// a choice

print 2;

Queue: 2, 4

Queue: 1

Queue: 4, 5

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 4;

Queue: 5

Queue: 2, 4

Queue: 1

Queue: 4, 5

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 4;

print 5;

Queue:

Queue: 5

Queue: 2, 4

Queue: 1

Queue: 4, 5

Queue: 3

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 4;

print 5;

// a choice

print 3;

Queue:

Queue: 5

Queue: 2, 4

Queue: 1

Queue: 4, 5

Queue: 3

Queue: 6

Sample Graph BFS

1

4

2

65

3bfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 4;

print 5;

// a choice

print 3;

print 6;Queue:

Queue: 5

Queue: 2, 4

Queue: 1

Queue: 4, 5

Queue: 3

Queue: 6

Queue:

Moral

BFS is a generalization like the level-order traversal on trees Also maintain internally a queue to

control the visit order Obtain a BFS forest when finished

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;

// a choice

print 3;

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;

// a choice

print 3;

print 6;

DFS Algorithmvoid dfsOneVertex (Tv start, tyVisit visit){ visit (start);

for (each adjacent vertex u of “start”) if (not visited u) dfsOneVertex (u, visit);}

DFS Algorithmvoid dfs (T g, poly start, tyVisit visit){ Tv startV = searchVertex (g, start); dfsOneVertex (startV, visit);

for (each vertex u in graph g) if (not visited u) dfsOneVertex (u, visit);}

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

dfs(1)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

dfs(1) => dfs(2)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

dfs(1) => dfs(2) => dfs(5)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;dfs(1) => dfs(2) => dfs(5) => dfs(4)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;dfs(1) => dfs(2) => dfs(5) => dfs(4) => dfs(2)???

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;dfs(1) => dfs(2) => dfs(5)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;dfs(1) => dfs(2)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;dfs(1)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;dfs(1) =>dfs(4)???

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;dfs(1)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;empty!

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;

// a choice

print 3;

dfs(3)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;

// a choice

print 3;

print 6;

dfs(3) =>dfs(6)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;

// a choice

print 3;

print 6;

dfs(3) =>dfs(6) =>dfs(6)???

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;

// a choice

print 3;

print 6;

dfs(3) =>dfs(6)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;

// a choice

print 3;

print 6;

dfs(3)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;

// a choice

print 3;

print 6;

dfs(3) =>dfs(5)???

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;

// a choice

print 3;

print 6;

dfs(3)

Sample Graph DFS

1

4

2

65

3dfs (g, 1, Int_print);

print 1;

// a choice

print 2;

print 5;

print 4;

// a choice

print 3;

print 6;

empty!

Moral

DFS is a generalization of the pre-order traversal on trees

Maintain internally a stack to control the visit order for recursion function, machine maintain

an implicit stack Obtain a DFS forest when finished

Edge Classification

Once we obtain the DFS (or BFS) spanning trees (forests), the graph edges could be classified according to the trees: tree edges: edges in the trees forward edges: ancestors to descants back edges: descants to ancestors cross edges: others

Edge Classification Example

1

4

2

65

3• tree edges:

1->2, 2->5, 5->4, 3->6

• forward edges:

1->4

• back edges:

4->2, 6->6

• cross edges:

3->5