Graphs
Graphs
• Directed Graphs
• Undirected Graphs
Terminology
• Two Algorithms
• BFS - Breadth First Search
• DFS - Depth First Search
BFS
Sunday, February 26, 2023 11:05 AM
BFS Example
M
BFS Analysis
DFS Analysis
Applications of BFS
Testing bipartiteness
Lemma : Let G be a connected graph, and let L0, …, Lk be the layers produced
by BFS starting at node s. Exactly one of the following holds :
(i) No edge of G joins two nodes of the same layer, and G is bipartite.
Lemma : Let G be a connected graph, and let L0, …, Lk be the layers produced
by BFS starting at node s. Exactly one of the following holds :
(i) No edge of G joins two nodes of the same layer, and G is bipartite.
(ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not
bipartite).
Proof (i)
Suppose no edge joins two nodes in same layer.
By BFS property, each edge join two nodes in adjacent levels.
Bipartition: white = nodes on odd levels, blue = nodes on even levels.
Proof (ii)
Suppose (x, y) is an edge with x, y in same level Lj.
Let z = lca(x, y) = lowest common ancestor.
Let Li be level containing z.
Consider cycle that takes edge from x to y, then path from y to z, then path from z to x.
Its length is 1 + (j – i) + (j – i), which is odd
Applications of DFS
Example :
Web crawler. Start from web page s. Find all web pages linked from s, either directly or
indirectly.
Strongly Connected Components of Directed Graph
Mutually reachable : Nodes u and v are mutually reachable if there is a both path from
u to v and also a path from v to u.
Lemma. Let s be any node. G is strongly connected iff every node is reachable from s,
and s is reachable from every node.
Pf. ⇒ Follows from definition.
Pf. ⇐ Path from u to v: concatenate u↝s path with s↝v path.
Path from v to u: concatenate v↝s path with s↝u path. ▪
Using BFS
• Pick any node s.
• Run BFS from s in G.
• Run BFS from s in Greverse.
• Return true iff all nodes reached in both BFS executions.
• Correctness follows immediately from previous lemma.
• Time complexity is O(m + n) time
Using DFS
• discovery time: d[u] = the time at which u is first visited
• finishing time: f [u] = the time at which all u and all its neighbors have been visited.
• Clearly d[u] < f [u].
• Every edge (u; v) in a DAG has f [v] < f [u].
• If we list nodes from largest f [u] to smallest f [u] then every edge goes from left to right.
• Exactly a topological sort.
• So: as each node is finished, add it to the front of a linked list.
Question
Following diagram represents a social network. A line between two people means that
they are connected on a meeting app. Message broadcasting is a chain reaction in this
network. When a person broadcasts a message to all his contacts, his contacts in turn
broadcast the message to all their contacts and so on. Write an algorithm to check
whether the message broadcasted by any given person 'x' in the network reaches to
each and every other person in the network. In the following instance show the path of
transmission from Audrey to all other persons using the algorithm. Also check
whether it is possible to divide people in this social network in two groups such that
people belonging to a group are not connected on the social media.