Download presentation
Presentation is loading. Please wait.
1
Graph Traversal C and Data Structures Baojian Hua bjhua@ustc.edu.cn
2
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
3
“ graph ” ADT in C: Interface // in file “graph.h” #ifndef GRAPH_H #define GRAPH_H #define T Graph_t typedef 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
4
Sample Graph 1 4 2 65 3
5
Sample Graph BFS 1 4 2 65 3 bfs (g, 1, Int_print);
6
Sample Graph BFS 1 4 2 65 3 bfs (g, 1, Int_print); print 1;
7
Sample Graph BFS 1 4 2 65 3 bfs (g, 1, Int_print); print 1; // a choice print 2;
8
Sample Graph BFS 1 4 2 65 3 bfs (g, 1, Int_print); print 1; // a choice print 2; print 4;
9
Sample Graph BFS 1 4 2 65 3 bfs (g, 1, Int_print); print 1; // a choice print 2; print 4; print 5;
10
Sample Graph BFS 1 4 2 65 3 bfs (g, 1, Int_print); print 1; // a choice print 2; print 4; print 5; // a choice print 3;
11
Sample Graph BFS 1 4 2 65 3 bfs (g, 1, Int_print); print 1; // a choice print 2; print 4; print 5; // a choice print 3; print 6;
12
BFS Algorithm void 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); } }
13
BFS Algorithm void 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); }
14
Sample Graph BFS 1 4 2 65 3 bfs (g, 1, Int_output); Queue: 1
15
Sample Graph BFS 1 4 2 65 3 bfs (g, 1, Int_output); print 1; Queue: 2, 4 Queue: 1
16
Sample Graph BFS 1 4 2 65 3 bfs (g, 1, Int_print); print 1; // a choice print 2; Queue: 2, 4 Queue: 1 Queue: 4, 5
17
Sample Graph BFS 1 4 2 65 3 bfs (g, 1, Int_print); print 1; // a choice print 2; print 4; Queue: 5 Queue: 2, 4 Queue: 1 Queue: 4, 5
18
Sample Graph BFS 1 4 2 65 3 bfs (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
19
Sample Graph BFS 1 4 2 65 3 bfs (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
20
Sample Graph BFS 1 4 2 65 3 bfs (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:
21
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
22
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print);
23
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1;
24
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2;
25
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5;
26
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4;
27
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; // a choice print 3;
28
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; // a choice print 3; print 6;
29
DFS Algorithm void dfsOneVertex (Tv start, tyVisit visit) { visit (start); for (each adjacent vertex u of “start”) if (not visited u) dfsOneVertex (u, visit); }
30
DFS Algorithm void 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); }
31
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; dfs(1)
32
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; dfs(1) => dfs(2)
33
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; dfs(1) => dfs(2) => dfs(5)
34
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; dfs(1) => dfs(2) => dfs(5) => dfs(4)
35
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; dfs(1) => dfs(2) => dfs(5) => dfs(4) => dfs(2)???
36
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; dfs(1) => dfs(2) => dfs(5)
37
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; dfs(1) => dfs(2)
38
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; dfs(1)
39
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; dfs(1) =>dfs(4)???
40
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; dfs(1)
41
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; empty!
42
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; // a choice print 3; dfs(3)
43
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; // a choice print 3; print 6; dfs(3) =>dfs(6)
44
Sample Graph DFS 1 4 2 65 3 dfs (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)???
45
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; // a choice print 3; print 6; dfs(3) =>dfs(6)
46
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; // a choice print 3; print 6; dfs(3)
47
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; // a choice print 3; print 6; dfs(3) =>dfs(5)???
48
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; // a choice print 3; print 6; dfs(3)
49
Sample Graph DFS 1 4 2 65 3 dfs (g, 1, Int_print); print 1; // a choice print 2; print 5; print 4; // a choice print 3; print 6; empty!
50
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
51
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
52
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.