PowerPoint Presentation - Topology Based Methods in Shape...
Transcript of PowerPoint Presentation - Topology Based Methods in Shape...
![Page 1: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/1.jpg)
CSE 2331/5331
CSE 2331/5331
Topic 9:Basic Graph Alg.
RepresentationsBasic traversal algorithmsTopological sort
![Page 2: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/2.jpg)
CSE 2331/5331
What Is A Graph
Graph G = (V, E) V: set of nodes E: set of edges
Example: V ={ a, b, c, d, e, f } E ={(a, b), (a, d), (a, e), (b, c), (b, d), (b, e), (c, e), (e,f)}
ba
f
c
d e
![Page 3: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/3.jpg)
Un-directed graph 𝐸𝐸 ≤ 𝑉𝑉
2
Directed graph 𝐸𝐸 ≤ 𝑉𝑉2
CSE 2331/5331
ba
f
c
d e
![Page 4: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/4.jpg)
Un-directed graphs
CSE 2331/5331
![Page 5: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/5.jpg)
Vertex Degree
deg(v) = degree of v = # edges incident on v
CSE 2331/5331
ba
f
c
d e
Lemma: ∑𝑣𝑣𝑖𝑖∈𝑉𝑉(𝐺𝐺) deg 𝑣𝑣𝑖𝑖 = 2|𝐸𝐸|
![Page 6: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/6.jpg)
Some Special Graphs
Complete graph Path Cycle Planar graph Tree
CSE 2331/5331
![Page 7: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/7.jpg)
CSE 2331/5331
Representations of Graphs
Adjacency lists Each vertex u has a list, recording its neighbors
i.e., all v’s such that (u, v) ∈ E
An array of V lists V[i].degree = size of adj list for node 𝑣𝑣𝑖𝑖 V[i].AdjList = adjacency list for node 𝑣𝑣𝑖𝑖
![Page 8: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/8.jpg)
CSE 2331/5331
Adjacency Lists
For vertex v ∈ V, its adjacency list has size: deg(v) decide whether (v, u) ∈ E or not in time O(deg(v))
Size of data structure (space complexity): O(|V| + |E|) = O(V+E)
![Page 9: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/9.jpg)
CSE 2331/5331
Adjacency Matrix
𝑉𝑉 × 𝑉𝑉 matrix 𝐴𝐴 𝐴𝐴 𝑖𝑖, 𝑗𝑗 = 1 if 𝑣𝑣𝑖𝑖 ,𝑣𝑣𝑗𝑗 is an edge Otherwise, 𝐴𝐴 𝑖𝑖, 𝑗𝑗 = 0
![Page 10: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/10.jpg)
CSE 2331/5331
Adjacency Matrix
Size of data structure: O ( V × V)
Time to determine if (v, u) ∈ E : O(1)
Though larger, it is simpler compared to adjacency list.
![Page 11: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/11.jpg)
Sample Graph Algorithm
Input: Graph G represented by adjacency lists
CSE 2331/5331
Running time:O(V + E)
![Page 12: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/12.jpg)
Connectivity
A path in a graph is a sequence of vertices 𝑢𝑢1,𝑢𝑢2, … ,𝑢𝑢𝑘𝑘 such that there is an edge 𝑢𝑢𝑖𝑖 ,𝑢𝑢𝑖𝑖+1 between any two adjacent vertices in the
sequence Two vertices 𝑢𝑢,𝑤𝑤 ∈ 𝑉𝑉 𝐺𝐺 are connected if there is a
path in G from u to w. We also say that w is reachable from u.
A graph G is connected if every pair of nodes 𝑢𝑢,𝑤𝑤 ∈𝑉𝑉 𝐺𝐺 are connected.
CSE 2331/5331
![Page 13: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/13.jpg)
Connectivity Checking
How to check if the graph is connected? One approach: Graph traversal
BFS: breadth-first search DFS: depth-first search
CSE 2331/5331
![Page 14: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/14.jpg)
CSE 2331/5331
BFS: Breadth-first search
Input: Given graph G = (V, E), and a source node s ∈ V
Output: Will visit all nodes in V reachable from s For each 𝑣𝑣 ∈ 𝑉𝑉, output a value 𝑣𝑣.𝑑𝑑
𝑣𝑣.𝑑𝑑 = distance (smallest # of edges) from s to v. 𝑣𝑣.𝑑𝑑 = ∞ if v is not reachable from s.
![Page 15: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/15.jpg)
CSE 2331/5331
Intuition
Starting from source node s, Spread a wavefront to visit other nodes First visit all nodes one edge away from s Then all nodes two edges away from s …
Need a data-structureto store nodes to be explored.
![Page 16: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/16.jpg)
CSE 2331/5331
Intuition cont.
A node can be: un-discovered discovered, but not explored explored (finished)
𝑣𝑣.𝑑𝑑 : is set when node v is first discovered.
Need a data structure to store discovered but un-explored nodes FIFO ! Queue
![Page 17: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/17.jpg)
CSE 2331/5331
Pseudo-code
Time complexity:O(V+E)
Use adjacency listrepresentation
![Page 18: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/18.jpg)
CSE 2331/5331
Correctness of Algorithm
A node not reachable from s will not be visited A node reachable from s will be visited 𝑣𝑣.𝑑𝑑 computed is correct:
Intuitively, if all nodes k distance away from s are in level k, and no other nodes are in level k,
Then all nodes (k+1)-distance away from s must be in level (k+1).
Rigorous proof by induction
![Page 19: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/19.jpg)
BFS tree
A node 𝑣𝑣 is the parent of 𝑢𝑢 if 𝑢𝑢 was first discovered when exploring 𝑣𝑣
A BFS tree 𝑇𝑇 Root: source node 𝑠𝑠 Nodes in level 𝑘𝑘 of 𝑇𝑇 are distance 𝑘𝑘 away from 𝑠𝑠
CSE 2331/5331
![Page 20: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/20.jpg)
Connectivity-Checking
CSE 2331/5331
Time complexity:O(V+E)
![Page 21: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/21.jpg)
CSE 2331/5331
Summary for BFS
Starting from source node s, visits remaining nodes of graph from small distance to large distance
This is one way to traverse an input graph With some special property where nodes are visited in
non-decreasing distance to the source node s. Return distance between s to any reachable node in
time O(|V| + |E|)
![Page 22: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/22.jpg)
DFS: Depth-First Search
Another graph traversal algorithm BFS:
Go as broad as possible in the algorithm DFS:
Go as deep as possible in the algorithm
CSE 2331/5331
![Page 23: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/23.jpg)
Example
Perform DFS starting from 𝑣𝑣1 What if we add edge 𝑣𝑣1, 𝑣𝑣8
CSE 2331/5331
![Page 24: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/24.jpg)
DFS
Time complexity O(V+E)
CSE 2331/5331
![Page 25: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/25.jpg)
Depth First Search Tree
If v is discovered when exploring u Set 𝑣𝑣.𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝 = 𝑢𝑢
The collection of edges 𝑣𝑣.𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝, 𝑣𝑣 form a tree, called Depth-first search tree.
CSE 2331/5331
![Page 26: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/26.jpg)
DFS with DFS Tree
CSE 2331/5331
![Page 27: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/27.jpg)
Example
Perform DFS starting from 𝑣𝑣1
CSE 2331/5331
![Page 28: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/28.jpg)
Another Connectivity Test
CSE 2331/5331
![Page 29: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/29.jpg)
Traverse Entire Graph
CSE 2331/5331
![Page 30: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/30.jpg)
Remarks
DFS(G, k) Another way to compute all nodes reachable to the
node 𝑣𝑣𝑘𝑘 Same time complexity as BFS There are nice properties of DFS and DFS tree
that we are not reviewing in this class.
CSE 2331/5331
![Page 31: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/31.jpg)
Directed Graphs
CSE 2331/5331
![Page 32: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/32.jpg)
Un-directed graph 𝐸𝐸 ≤ 𝑉𝑉
2
Directed graph Each edge 𝑢𝑢, 𝑣𝑣 is directed from u to v 𝐸𝐸 ≤ 𝑉𝑉2
CSE 2331/5331
ba
f
c
d e
![Page 33: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/33.jpg)
Vertex Degree
indeg(v) = # edges of the form 𝑢𝑢, 𝑣𝑣 outdeg(v) = # edges of the form 𝑣𝑣,𝑢𝑢
CSE 2331/5331
Lemma: ∑𝑣𝑣𝑖𝑖∈𝑉𝑉(𝐺𝐺) indeg 𝑣𝑣𝑖𝑖 = |𝐸𝐸| Lemma: ∑𝑣𝑣𝑖𝑖∈𝑉𝑉(𝐺𝐺) outdeg 𝑣𝑣𝑖𝑖 = |𝐸𝐸|
ba
f
c
d e
![Page 34: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/34.jpg)
CSE 2331/5331
Representations of Graphs
Adjacency lists Each vertex u has a list, recording its neighbors
i.e., all v’s such that (u, v) ∈ E
An array of V lists V[i].degree = size of adj list for node 𝑣𝑣𝑖𝑖 V[i].AdjList = adjacency list for node 𝑣𝑣𝑖𝑖
![Page 35: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/35.jpg)
CSE 2331/5331
Adjacency Lists
For vertex v ∈ V, its adjacency list has size: outdeg(v) decide whether (v, u) ∈ E or not in time O(outdeg(v))
Size of data structure (space complexity): O(V+E)
![Page 36: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/36.jpg)
CSE 2331/5331
Adjacency Matrix
𝑉𝑉 × 𝑉𝑉 matrix 𝐴𝐴 𝐴𝐴 𝑖𝑖, 𝑗𝑗 = 1 if 𝑣𝑣𝑖𝑖 ,𝑣𝑣𝑗𝑗 is an edge Otherwise, 𝐴𝐴 𝑖𝑖, 𝑗𝑗 = 0
![Page 37: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/37.jpg)
CSE 2331/5331
Adjacency Matrix
Size of data structure: O ( V × V)
Time to determine if (v, u) ∈ E : O(1)
Though larger, it is simpler compared to adjacency list.
![Page 38: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/38.jpg)
Sample Graph Algorithm
Input: Directed graph G represented by adjacency list
CSE 2331/5331
Running time:O(V + E)
![Page 39: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/39.jpg)
Connectivity
A path in a graph is a sequence of vertices 𝑢𝑢1,𝑢𝑢2, … ,𝑢𝑢𝑘𝑘 such that there is an edge 𝑢𝑢𝑖𝑖 ,𝑢𝑢𝑖𝑖+1 between any two adjacent vertices in the
sequence Given two vertices 𝑢𝑢,𝑤𝑤 ∈ 𝑉𝑉 𝐺𝐺 , we say that w is
reachable from u if there is a path in G from u to w. Note: w is reachable from u DOES NOT necessarily mean
that u is reachable from w.
CSE 2331/5331
![Page 40: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/40.jpg)
Reachability Test
How many (or which) vertices are reachable from a source node, say 𝑣𝑣1?
CSE 2331/5331
![Page 41: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/41.jpg)
CSE 2331/5331
BFS and DFS
The algorithms for BFS and DFS remain the same Each edge is now understood as a directed edge
BFS(V,E, s) : visits all nodes reachable from s in non-decreasing
order
![Page 42: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/42.jpg)
CSE 2331/5331
BFS
Starting from source node s, Spread a wavefront to visit other nodes First visit all nodes one edge away from s Then all nodes two edges away from s …
![Page 43: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/43.jpg)
CSE 2331/5331
Pseudo-code
Time complexity:O(V+E)
Use adjacency listrepresentation
![Page 44: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/44.jpg)
Number of Reachable Nodes
CSE 2331/5331
Time complexity:O(V+E)
Compute # nodesreachable from 𝑣𝑣𝑘𝑘
![Page 45: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/45.jpg)
DFS: Depth-First Search
Similarly, DFS remains the same Each edge is now a directed edge
If we start with all nodes unvisited, Then DFS(G, 𝑘𝑘) visits all nodes reachable to node 𝑣𝑣𝑘𝑘
BFS from previous NumReachable() procedure can be replaced with DFS.
CSE 2331/5331
![Page 46: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/46.jpg)
More Example
Is 𝑣𝑣1 reachable from 𝑣𝑣12 ? Is 𝑣𝑣12 reachable from 𝑣𝑣1 ?
CSE 2331/5331
![Page 47: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/47.jpg)
DFS above can be replaced with BFS
CSE 2331/5331
DFS(G, k);
![Page 48: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/48.jpg)
Topological Sort
CSE 2331/5331
![Page 49: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/49.jpg)
CSE 2331/5331
Directed Acyclic Graph
A directed cycle is a sequence 𝑢𝑢1,𝑢𝑢2, … ,𝑢𝑢𝑘𝑘 ,𝑢𝑢1such that there is a directed edge between any two consecutive nodes.
DAG: directed acyclic graph Is a directed graph with no directed cycles.
![Page 50: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/50.jpg)
CSE 2331/5331
Topological Sort
A topological sort of a DAG G = (V, E) A linear ordering A of all vertices from V If edge (u,v) ∈ E => A[u] < A[v]
undershorts
pants
beltshirt
tie
jacket
shoes
sockswatch
![Page 51: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/51.jpg)
Another Example
CSE 2331/5331
Is the sorting order unique?
Why requires DAG?
![Page 52: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/52.jpg)
A topological sorted order of graph G exists if and only if G is a directed acyclic graph (DAG).
CSE 2331/5331
![Page 53: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/53.jpg)
Question
How to topologically sort a given DAG? Intuition:
Which node can be the first node in the topological sort order?
A node with in-degree 0 ! After we remove this, the process can be repeated.
CSE 2331/5331
![Page 54: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/54.jpg)
Example
CSE 2331/5331
undershorts
pants
beltshirt
tie
jacket
shoes
sockswatch
![Page 55: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/55.jpg)
CSE 2331/5331
![Page 56: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/56.jpg)
Topological Sort – Simplified Implementation
CSE 2331/5331
![Page 57: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/57.jpg)
Time complexity O(V+E)
Correctness: What if the algorithm terminates before we finish
visiting all nodes? Procedure TopologicalSort(G) outputs a sorted list of
all nodes if and only if the input graph G is a DAG If G is not DAG, the algorithm outputs only a partial list of
vertices.
CSE 2331/5331
![Page 58: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/58.jpg)
Remarks
Other topological sort algorithm by using properties of DFS
CSE 2331/5331
![Page 59: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/59.jpg)
Last
Analyzing graph algorithms Adjacency list representation or Adjacency matrix representation
CSE 2331 / 5331
![Page 60: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/60.jpg)
Edge-weighted un-directed graph G = (V, E) and edge weight function 𝑤𝑤:𝐸𝐸 → 𝑅𝑅 E.g, road network, where each node is a city, and each
edge is a road, and weight is the length of this road.
CSE 2331 / 5331
3 5ba
f
c
d e
1 1
42
11 2
![Page 61: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/61.jpg)
Example 1
CSE 2331 / 5331
Assume G is represented by adjacency list
Q is priority-queue implemented by min-heap
![Page 62: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec9.pdfPowerPoint Presentation - Topology Based Methods in Shape](https://reader035.fdocuments.in/reader035/viewer/2022081517/60007aa3c90acd2dd70aa0eb/html5/thumbnails/62.jpg)
Example 2
CSE 2331 / 5331
Assume G is represented by adjacency matrix
What if move line 10 to above line 8?
What if move line 10 to above line 9?