0% found this document useful (0 votes)
14 views27 pages

Lec5 Handout

1. A directed graph models relationships where connections have direction, like links between web pages. 2. A graph is strongly connected if there is a directed path between any two nodes. It forms a strongly connected component (SCC). 3. The nodes in an SCC are maximally connected - there is no path between nodes in different SCCs.

Uploaded by

Anh Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views27 pages

Lec5 Handout

1. A directed graph models relationships where connections have direction, like links between web pages. 2. A graph is strongly connected if there is a directed path between any two nodes. It forms a strongly connected component (SCC). 3. The nodes in an SCC are maximally connected - there is no path between nodes in different SCCs.

Uploaded by

Anh Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

COMPSCI 311 Introduction to Algorithms

Lecture 5: Graphs: Bipartite, Directed, Topological Sorting

Marius Minea and Ghazaleh Parvini

University of Massachusetts Amherst

22 February 2023

slides credit: Dan Sheldon, Akshay Krishnamurthy, Andrew McGregor


Review: BFS/DFS
▶ Different versions of general exploration strategy

▶ O(m + n) time

▶ Basic algorithmic primitive — used in many other algorithms

▶ Check connectivity:
existence of path between two nodes
BFS: find shortest path
divide into connected components

▶ Check for cycles (any non-tree edge)

▶ Produce trees with useful properties (for other problems):


BFS: non-tree edge: at most one layer difference
DFS: non-tree edge goes to ancestor (> 1 level up)
Today

▶ Bipartite testing

▶ Directed graphs
▶ Traversal
▶ Strong connectivity
▶ Topological sorting
Bipartite Graphs

Definition Graph G = (V, E) is bipartite if V can be partitioned


into sets X, Y such that every edge has one end in X and one in Y .

Can color nodes red/blue s.t. no edges between nodes of same color.

Examples
▶ student-college graph in stable matching
▶ client-server connections
Bipartite Graphs and Odd Cycles

Are these graphs bipartite?

Yes! No, cycle with 5 nodes (odd)


Claim (easy): If G contains an odd cycle, it is not bipartite.
Proof: On cycle with 2k + 1 nodes, v1 , v2 , v3 , v4 , . . . v2k+1 must
alternate between different sets. But there is an edge v2k+1 — v1 .
Contrapositive: If G is bipartite, it does not contain odd cycles.
Testing Bipartiteness

Question Given G = (V, E), is G bipartite?

Idea: Use alternate coloring, but not on individual cycles.


Use alternating color “waves” from a start node =⇒ BFS
▶ L0 : red
▶ L1 : edges from node in L0 =⇒ blue
▶ L2 : edges from node in L1 =⇒ red
▶ ...
▶ Even layers red, odd layers blue
This colors all nodes. What could go wrong?
edge between two nodes at same layer.
Bipartite Testing: Algorithm

Run BFS from any node s


if there is an edge between two nodes in same layer then
Output "not bipartite"
else
X = even layers
Y = odd layers

Correctness? Recall: all edges between same or adjacent layers.


1. If there are no edges between nodes in the same layer, then
G is bipartite. (Obvious, all other edges are to adjacent layer)
2. If there is an edge between two nodes in the same layer,
G has an odd cycle and is not bipartite. Proof?
Bipartite Testing: Proof

▶ Let T be BFS tree of G and suppose (x, y) is an edge between


two nodes in the layer j
▶ Let z ∈ Li be the least common ancestor of x and y
(Useful in proofs: take least/greatest item with some property)

z ▶ Pzx = path from z to x in T


layer i
▶ Pyz = path from z to y in T
▶ Cycle following Pzx , then edge (x, y)
then Pyz has length 2(j − i) + 1,
x y layer j which is odd

▶ Therefore G is not bipartite.


Review: What Did We Prove?

Not SameLayerEdge =⇒ Bipartite (color in BFS)

SameLayerEdge ⇐= Not Bipartite (contrapositive)


SameLayerEdge =⇒ OddCycle =⇒ Not Bipartite
(BFS path “triangle”) (alternate coloring fails)

Cycle of 3 implications =⇒ three equivalent properties.


Directed Graphs

G = (V, E)
▶ (u, v) ∈ E is a directed edge
▶ u points to v

Examples
▶ Facebook: undirected
▶ Twitter: directed
▶ Web: directed
▶ Road network: directed (if one-way roads)
We may have both a u → v and a v → u edge
Directed Graph Definitions

Most definitions extend naturally to directed graphs by mapping the


word “edge” to “directed edge”
▶ Directed path: sequence P = v1 , v2 , . . . , vk−1 , vk such that
each consecutive pair vi , vi+1 is joined by a directed edge in G.
A v1 → vk path.

▶ Directed cycle: directed path, v1 = vk , other nodes distinct

▶ Connected? Connected component? More subtle, because


now there can be a path from s to t but not vice versa.

▶ Weakly connected: if it is connected when considering edges


undirected
Directed Graph Traversal

Reachability. Find all nodes reachable from some node s.

s-t shortest path.


What is the length of the shortest directed path from s to t?

Algorithm? BFS naturally extends to directed graphs.


Add v to Li+1 if there is a directed edge from Li and v is not
already discovered.
BFS in Directed Graph

BFS/DFS naturally extend to directed graphs.


BFS(s):
mark s as "discovered"
L[0] ← {s}, i ← 0
while L[i] is not empty do
L[i + 1] ← empty list
for all nodes v in L[i] do
for all edges (v, w) leaving v do
if w is not marked "discovered" then
mark w as "discovered"
put w in L[i + 1]
i←i+1
Traversal Variations: Reverse

Traversal from s finds nodes t with path s ⇝ t


There may be no path t ⇝ s

Find all nodes v from which we can reach t? (v → t path)?


BFS following edges in reverse direction

Useful to keep adjacency lists for both outgoing and incoming edges.
Traversing Directed vs. Undirected Graphs

Recall: Tree = undirected, connected, acyclic graph


⇒ finding a non-tree edge (in BFS or DFS) = cycle
Non-tree edge: reaching an already discovered node
(except for node’s parent)

1
No longer true in directed graph:
2 3
BFS in Directed Graphs

Non-tree edges can go:


▶ one level down why not > 1 ?
same reason, would add node to next level
▶ to the same level
▶ any number of levels up
BFS is not suitable for checking directed cycles.
DFS in Directed Graphs

1
1 7
2
2 3
3 4
4 5 8
5
6
6

3 → 1 is a back edge (to ancestor)


2 → 5 is a forward edge (to descendant)
4 → 5 is a cross edge (node in another subtree)
7, 8 not reachable, must restart search (DFS forest)
Edge Types in Directed DFS

To detect the various edges, we track:


▶ start (“discovered”) / end (“explored”) of neighbor iteration
▶ order in which nodes are reached (running counter)
count = 0
DFS(u)
num[u] = ++count
mark u as "discovered"
for all edges (u, v) do
if v is not marked then
call DFS(v) recursively ▷ tree edge
else if v is "discovered" then ▷ back edge
else ▷ v is "explored"
if num[v] > num[u] then ▷ forward edge
else ▷ num[v] < num[u]: cross edge
mark u as "explored"
Example: DFS Edges

A1

B2 C3

D4 E5

▶ tree edge: v not marked (first seen) A→C, C→D, etc.


▶ back edge: v discovered (not explored) D→A
▶ forward edge, when v explored, num[v] > num[u]
u was reached before v, u is ancestor of v A→E
▶ cross edge, when v explored, num[v] < num[u]
v was reached before u, in different subtrees E→D
Graph has a cycle iff we find a back edge
13.3. THE WEB AS A DIRECTED GRAPH 387

Directed Graph Connectivity


I'm a student I'm a applying to
at Univ. of X college

Univ. of X
My song
lyrics Strongly connected graph.
Classes USNews:
Directed path between any
College
Rankings two nodes.
I teach at
Networks
Univ. of X
Strongly connected
USNews:

Networks
Featured
Colleges
component (SCC).
class blog
Maximal subset of nodes
Blog post about
college rankings with directed path between
any two.
Blog post
Company Z's
about
Company Z
home page
SCCs can be found in time
Our Press
O(m + n). (Tarjan, 1972)
Founders Releases

Contact Us

Figure 13.6: A directed graph with its strongly connected components identified.
Directed Acyclic Graphs
Definition
A directed acyclic graph (DAG) is a directed graph with no cycles.

Models dependencies, e.g. course prerequisites:

CS220

CS187 CS311

CS240

Math132 CS383

CS250

Math: (strict) partial order (irreflexive, antisymmetric, transitive)


Topological Ordering

Definition A topological ordering of a directed graph is an ordering


of the nodes such that all edges go “forward” in the ordering
▶ Label nodes v1 , v2 , . . . , vn such that
for all edges (vi , vj ) we have i < j
▶ A way to order classes so all prerequisites are satisfied

Q: Is a topological ordering possible for any graph?


Claim If G has a topological ordering, then G is a DAG.
Proof by contradiction. Assume G has a cycle C; let vi be the
smallest-index node on C, and vj the node before it on C.
Then j > i and there is an edge vj → vi , contradiction.
Topological Ordering

CS220

CS187 CS311

CS240

Math132 CS383

CS250

1. Can we find a topological ordering in this graph?


2. Can we devise a general algorithm? 1. pick sth that has no
incoming edge
Topological Ordering

CS220

CS187 CS311

CS240

Math132 CS383

CS250

M132 C187 C220 C240 C250 C311 C383


Topological Sorting

Problem Given DAG G, compute a topological ordering for G.

topo-sort(G)
while there are nodes remaining do
Find a node v with no incoming edges
Place v next in the order
Delete v and all of its outgoing edges from G

Running time? O(n2 + m) easy. O(m + n) more clever


Correctness proof?
Topological Sorting Analysis

▶ In a DAG, there is always a node v with no incoming edges.


Try to prove by contradiction.
Otherwise, we can go backwards from every node;
after at most n + 1 nodes, one repeats, closing a cycle.

▶ Any node with no incoming edges can be first in topological


ordering.

▶ Removing this node v from a DAG G produces a new DAG G′

▶ Adding v before a topological ordering for G′ is a topological


ordering for G (inductive step).

Theorem: G is a DAG if and only if G has a topological ordering.


Topological Sorting in O(m + n)

topo-sort(G)
while there are nodes remaining do ▷ n iterations
Find a node v with no incoming edges ▷ O(n)/iteration?
Place v next in the order ▷ O(1)/iteration
Delete v and its outgoing edges from G ▷ O(m) overall

Optimization: don’t search every time for nodes w/o incoming edges
▶ Keep and update incoming edge count for each node:
setup in O(m + n), each update constant-time
▶ Keep set of nodes with no incoming edges;
add node to set when its count becomes zero
▶ Running time: O(m + n)

You might also like