Professional Documents
Culture Documents
CS 512: Data Structures and Algorithms: Dr. Sushanta Karmakar
CS 512: Data Structures and Algorithms: Dr. Sushanta Karmakar
Graphs
Graphs
Graphs
Graphs
G is a way of encoding pairwaise relationship G = (V , E ), Directed / undirected graph For directed edge (u, v ), it means the edge leaves u and
enters v
Examples
Transportation Network
Examples
Examples
Transportation Network Communication Network Information Network (there is an edge from u to v if u has a
hyperlink to v )
Examples
Transportation Network Communication Network Information Network (there is an edge from u to v if u has a
hyperlink to v )
Social Network
Examples
Transportation Network Communication Network Information Network (there is an edge from u to v if u has a
hyperlink to v )
Social Network Dependency Network (course prerequisites)
Connectivity
Connectivity
Connectivity
Tree
contain a cycle
Tree
contain a cycle
delete some edge, tree becomes a disconnected graph
Tree
contain a cycle
delete some edge, tree becomes a disconnected graph notion of parent and child
Tree
contain a cycle
delete some edge, tree becomes a disconnected graph notion of parent and child descendant / ancestor
Tree
contain a cycle
delete some edge, tree becomes a disconnected graph notion of parent and child descendant / ancestor Leaf: node with no descendant
Tree
contain a cycle
delete some edge, tree becomes a disconnected graph notion of parent and child descendant / ancestor Leaf: node with no descendant Every n node tree has exactly (n 1) edges
Graph Traversal
path from s to t
Graph Traversal
path from s to t
s t connectivity problem
Graph Traversal
path from s to t
s t connectivity problem Two natural algorithms: BFS (breadth-rst search) / DFS
(depth-rst search)
Breadth-First Search
Breadth-First Search
Breadth-First Search
start from node s search by layers L1 , L2 , L3 so on until no further node can be added
Breadth-First Search
start from node s search by layers L1 , L2 , L3 so on until no further node can be added Li is the i th hop neighbors
Breadth-First Search
start from node s search by layers L1 , L2 , L3 so on until no further node can be added Li is the i th hop neighbors distance from s to any node in Lj is j
Breadth-First Search
start from node s search by layers L1 , L2 , L3 so on until no further node can be added Li is the i th hop neighbors distance from s to any node in Lj is j There is a path from s to t if t appears in some layer
from s
from s
We call T as a breadth-rst search tree (simply BFS tree)
from s
We call T as a breadth-rst search tree (simply BFS tree) Try an example
A Property
A Property
belonging to layers Li and Lj respectively, let (x, y ) E such that (x, y ) is a nontree edge. Then i and j dier by at most 1.
A Property
belonging to layers Li and Lj respectively, let (x, y ) E such that (x, y ) is a nontree edge. Then i and j dier by at most 1.
proof by contradiction
Connected Component
Connected Component
Connected Component