A search space of graph motifs for graph compression: from ...
Graph Search Methods
-
Upload
sarah-todd -
Category
Documents
-
view
26 -
download
1
description
Transcript of Graph Search Methods
![Page 1: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/1.jpg)
Graph Search Methods• A vertex u is reachable from vertex v iff there is a
path from v to u.
23
8
10
1
45
9
116
7
![Page 2: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/2.jpg)
Graph Search Methods• A search method starts at a given vertex v and
visits/labels/marks every vertex that is reachable from v.
23
8
10
1
45
9
116
7
![Page 3: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/3.jpg)
Graph Search Methods• Many graph problems solved using a search
method. Path from one vertex to another. Is the graph connected? Find a spanning tree. Etc.
• Commonly used search methods: Breadth-first search. Depth-first search.
![Page 4: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/4.jpg)
Breadth-First Search
• Visit start vertex and put into a FIFO queue.
• Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue.
![Page 5: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/5.jpg)
Breadth-First Search Example
Start search at vertex 1.
23
8
10
1
45
9
116
7
![Page 6: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/6.jpg)
Breadth-First Search Example
Visit/mark/label start vertex and put in a FIFO queue.
23
8
10
1
45
9
116
7
1
FIFO Queue
1
![Page 7: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/7.jpg)
Breadth-First Search Example
Remove 1 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue
1
![Page 8: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/8.jpg)
Breadth-First Search Example
Remove 1 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
2
4
4
![Page 9: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/9.jpg)
Breadth-First Search Example
Remove 2 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
2
4
4
![Page 10: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/10.jpg)
Breadth-First Search Example
Remove 2 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
4
4
5
53
3
6
6
![Page 11: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/11.jpg)
Breadth-First Search Example
Remove 4 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
4
4
5
53
3
6
6
![Page 12: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/12.jpg)
Breadth-First Search Example
Remove 4 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
53
3
6
6
![Page 13: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/13.jpg)
Breadth-First Search Example
Remove 5 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
53
3
6
6
![Page 14: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/14.jpg)
Breadth-First Search Example
Remove 5 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
3
3
6
6
9
9
7
7
![Page 15: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/15.jpg)
Breadth-First Search Example
Remove 3 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
3
3
6
6
9
9
7
7
![Page 16: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/16.jpg)
Breadth-First Search Example
Remove 3 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
3
6
6
9
9
7
7
![Page 17: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/17.jpg)
Breadth-First Search Example
Remove 6 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
3
6
6
9
9
7
7
![Page 18: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/18.jpg)
Breadth-First Search Example
Remove 6 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
3
6
9
9
7
7
![Page 19: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/19.jpg)
Breadth-First Search Example
Remove 9 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
3
6
9
9
7
7
![Page 20: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/20.jpg)
Breadth-First Search Example
Remove 9 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
3
6
9
7
78 8
![Page 21: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/21.jpg)
Breadth-First Search Example
Remove 7 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
3
6
9
7
78 8
![Page 22: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/22.jpg)
Breadth-First Search Example
Remove 7 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
3
6
9
7
8 8
![Page 23: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/23.jpg)
Breadth-First Search Example
Remove 8 from Q; visit adjacent unvisited vertices;
put in Q.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
3
6
9
7
8 8
![Page 24: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/24.jpg)
Breadth-First Search Example
Queue is empty. Search terminates.
23
8
10
1
45
9
116
7
1
FIFO Queue2
45
3
6
9
7
8
![Page 25: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/25.jpg)
Breadth-First Search Property
• All vertices reachable from the start vertex (including the start vertex) are visited.
![Page 26: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/26.jpg)
Time Complexity
• Each visited vertex is put on (and so removed from) the queue exactly once.
• When a vertex is removed from the queue, we examine its adjacent vertices. O(n) if adjacency matrix used O(vertex degree) if adjacency lists used
• Total time O(mn), where m is number of vertices in the
component that is searched (adjacency matrix)
![Page 27: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/27.jpg)
Time Complexity
O(n + sum of component vertex degrees) (adj. lists)
= O(n + number of edges in component)
![Page 28: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/28.jpg)
Path From Vertex v To Vertex u
• Start a breadth-first search at vertex v.
• Terminate when vertex u is visited or when Q becomes empty (whichever occurs first).
• Time O(n2) when adjacency matrix used O(n+e) when adjacency lists used (e is number
of edges)
![Page 29: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/29.jpg)
Is The Graph Connected?
• Start a breadth-first search at any vertex of the graph.
• Graph is connected iff all n vertices get visited.
• Time O(n2) when adjacency matrix used O(n+e) when adjacency lists used (e is number
of edges)
![Page 30: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/30.jpg)
Connected Components
• Start a breadth-first search at any as yet unvisited vertex of the graph.
• Newly visited vertices (plus edges between them) define a component.
• Repeat until all vertices are visited.
![Page 31: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/31.jpg)
Connected Components
23
8
10
1
45
9
116
7
![Page 32: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/32.jpg)
Time Complexity
O(n2) when adjacency matrix used O(n+e) when adjacency lists used (e
is number of edges)
![Page 33: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/33.jpg)
Spanning Tree
Breadth-first search from vertex 1.
23
81
45
9
67
1
2
4
3
6
59
7
8
Breadth-first spanning tree.
![Page 34: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/34.jpg)
Spanning Tree
• Start a breadth-first search at any vertex of the graph.
• If graph is connected, the n-1 edges used to get to unvisited vertices define a spanning tree (breadth-first spanning tree).
• Time O(n2) when adjacency matrix used O(n+e) when adjacency lists used (e is number
of edges)
![Page 35: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/35.jpg)
Depth-First Search
depthFirstSearch(v)
{
Label vertex v as reached.
for (each unreached vertex u
adjacenct from v)
depthFirstSearch(u);
}
![Page 36: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/36.jpg)
Depth-First Search Example
Start search at vertex 1.
23
8
10
1
45
9
116
7
Label vertex 1 and do a depth first search from either 2 or 4.
1
2
Suppose that vertex 2 is selected.
![Page 37: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/37.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
Label vertex 2 and do a depth first search from either 3, 5, or 6.
1
22
5
Suppose that vertex 5 is selected.
![Page 38: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/38.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
Label vertex 5 and do a depth first search from either 3, 7, or 9.
1
22
559
Suppose that vertex 9 is selected.
![Page 39: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/39.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
Label vertex 9 and do a depth first search from either 6 or 8.
1
22
5599
8
Suppose that vertex 8 is selected.
![Page 40: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/40.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
Label vertex 8 and return to vertex 9.
1
22
5599
88
From vertex 9 do a dfs(6).
6
![Page 41: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/41.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
1
22
5599
88
Label vertex 6 and do a depth first search from either 4 or 7.
66
4
Suppose that vertex 4 is selected.
![Page 42: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/42.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
1
22
5599
88
Label vertex 4 and return to 6.
66
44
From vertex 6 do a dfs(7).
7
![Page 43: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/43.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
1
22
5599
88
Label vertex 7 and return to 6.
66
44
77
Return to 9.
![Page 44: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/44.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
1
22
5599
88
66
44
77
Return to 5.
![Page 45: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/45.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
1
22
5599
88
66
44
77
Do a dfs(3).
3
![Page 46: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/46.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
1
22
5599
88
66
44
77
Label 3 and return to 5.
33
Return to 2.
![Page 47: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/47.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
1
22
5599
88
66
44
77
33
Return to 1.
![Page 48: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/48.jpg)
Depth-First Search Example2
3
8
10
1
45
9
116
7
1
22
5599
88
66
44
77
33
Return to invoking method.
![Page 49: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/49.jpg)
Depth-First Search Properties• Same complexity as BFS.• Same properties with respect to path finding,
connected components, and spanning trees.• Edges used to reach unlabeled vertices define
a depth-first spanning tree when the graph is connected.
• There are problems for which bfs is better than dfs and vice versa.
![Page 50: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/50.jpg)
Shortest Path Problems
• Directed weighted graph.
• Path length is sum of weights of edges on path.
• The vertex at which the path begins is the source vertex.
• The vertex at which the path ends is the destination vertex.
![Page 51: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/51.jpg)
Example
A path from 1 to 7.
1
2
3
4
5
6
7
2
616
7
8
10
3
14
44
5 3
11
7
Path length is 14.
![Page 52: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/52.jpg)
Example
Another path from 1 to 7.
1
2
3
4
5
6
7
2
616
7
8
10
3
14
44
5 3
1
Path length is 11.
![Page 53: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/53.jpg)
Shortest Path Problems
• Single source single destination.
• Single source all destinations.
• All pairs (every vertex is a source and destination).
![Page 54: Graph Search Methods](https://reader036.fdocuments.in/reader036/viewer/2022062408/56813392550346895d9aa209/html5/thumbnails/54.jpg)
Single Source Single Destination
Possible greedy algorithm: Leave source vertex using cheapest/shortest edge. Leave new vertex using cheapest edge subject to the
constraint that a new vertex is reached. Continue until destination is reached.