Graph Algorithms: Text Book: Introduction To Algorithms Byclrs

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 142

Graph Algorithms

Text Book: Introduction to algorithms


By C L R S
Graphs - Definitions
• A directed graph (or digraph) G is a pair (V,E),
where V is a finite set and E is a binary relation
on V .
• The set V is called the vertex set of G, and its
elements are called vertices
• The set E is called the edge set of G, and its
elements are called edges.
• Vertices are usually represented by circles and
edges are represented by arrows.
Graphs - Definitions
• In an undirected graph G = (V,E), the edge set E
consists of unordered pairs of vertices, rather
than ordered pairs.
• If (u,v) is an edge in a directed graph G = (V,E),
we say that (u,v) is incident from or leaves
vertex u and is incident to or enters vertex v.
• If (u,v) is an edge in an undirected graph G
=(V,E), we say that (u,v) is incident on vertices u
and v.
Graphs - Definitions
• If (u,v) is an edge in a graph G =(V,E), we say
that vertex v is adjacent to vertex u.
• When the graph is undirected, the adjacency
relation is symmetric.
• The degree of a vertex in an undirected graph
is the number of edges incident on it.
• A vertex whose degree is 0 is isolated.
Graphs - Definitions
• In a directed graph, the out-degree of a vertex
is the number of edges leaving it, and the in-
degree of a vertex is the number of edges
entering it.
• The degree of a vertex in a directed graph is its
in-degree plus its out-degree.
Graphs - Definitions
• A path of length k from a vertex u to a vertex u’
in a graph G=(V,E) is a sequence <v0, v1, . . . ,vk>
of vertices such that u=v0, u’=vk, and (vi-1,vi) є E
for i= 1, 2, 3, . . . , k.
• An undirected graph is connected if every
vertex is reachable from all other vertices. The
connected components of a graph are the
equivalence classes of vertices under the “is
reachable from” relation.
Graphs - Definitions
• An undirected graph is connected if it has
exactly one connected component.
• A directed graph is strongly connected if every
two vertices are reachable from each other.
Representation of graphs
Representation of graphs
Representation of graphs
Representation of graphs
BFS
BFS
BFS Example
BFS Example
BFS Example
Properties of BFS
• Running time O(V + E)
• BFS computes shorted path distance
• Algorithm BFS builds a breadth first tree.
Printing shortest path
DFS
DFS
DFS Example
DFS Example
DFS Example
DFS Example
DFS Example
DFS – Example 2
DFS – Example 2
parenthesis theorem
In any depth-first search of a (directed undirected) graph
G=(V,E), for any two vertices u and v, exactly one of the
following three conditions holds:
– the intervals [u.d, u.f] and [v.d, v.f] are entirely disjoint, and
neither u nor v is a descendant of the other in the depth-first
forest,
– the interval [u.d, u.f] is contained entirely within the interval
[v.d, v.f], and u is a descendant of v in a depth-first tree, or
– the interval [v.d, v.f] is contained entirely within the interval
[u.d, u.f], and v is a descendant of u in a depth-first tree.
Classification of edges
• Tree edges are edges in the depth-first forest Gpi. Edge (u,v) is
a tree edge if v was first discovered by exploring edge (u,v)
• Back edges are those edges (u,v) connecting a vertex u to an
ancestor v in a depth-first tree. We consider self-loops, which
may occur in directed graphs, to be back edges.
• Forward edges are those nontree edges (u,v) connecting a
vertex u to a descendant v in a depth-first tree.
• Cross edges are all other edges. They can go between vertices
in the same depth-first tree, as long as one vertex is not an
ancestor of the other, or they can go between vertices in
different depth-first trees.
Classification of edges
when we first explore an edge (u,v), the color of
vertex v
– WHITE indicates a tree edge,
– GRAY indicates a back edge, and
– BLACK indicates a forward or cross edge.
Topological sort
Topological sort - algorithm
Topological sort
Strongly connected components
Strongly connected component of a directed
graph G = (V,E) is a maximal set of vertices C
where C is a subset of V such that for every
pair of vertices u and v in C, vertices u and v
are reachable from each other.
Strongly connected components- Algorithm

STRONGLY-CONNECTED-COMPONENTS(G)
1 call DFS(G) to compute finishing times u.f for each
vertex u
2 compute GT
3 call DFS(GT), but in the main loop of DFS, consider the
vertices in order of decreasing u.f (as computed in line
1)
4 output the vertices of each tree in the depth-first forest
formed in line 3 as a separate strongly connected
component
Strongly connected components- Example
Strongly connected components- Example
Component Graph
Minimal Spanning tree
Minimal Spanning tree
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm
• Running time O(E lg V)
Prim’s Algorithm
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Generic MST Algorithm
Light Edge
• A cut (S, V – S) of an undirected graph G=(V,E) is
a partition of V .
• An edge (u,v) є E crosses the cut (S, V-S) if one of
its endpoints is in S and the other is in V - S.
• A cut respects a set A of edges if no edge in A
crosses the cut.
• An edge is a light edge crossing a cut if its weight
is the minimum of any edge crossing the cut.
Safe Edge - Theorem
Let G=(V,E) be a connected, undirected graph
with a real-valued weight function w defined
on E. Let A be a subset of E that is included in
some minimum spanning tree for G, let (S, V-
S) be any cut of G that respects A, and let
(u,v) be a light edge crossing (S, V-S). Then,
edge (u,v) is safe for A.
Cut - Example
Cut – Another View
Shortest Path
• The weight w(p) of path p = <v0, v1, . . . , vk> is
the sum of the weights of its constituent
edges:
Shortest Path Problems
• single-source shortest-paths problem
• Single-destination shortest-paths
problem
• Single-pair shortest-path problem
• All-pairs shortest-paths problem
Shortest Path Problems
• Property
– Subpaths of shortest paths are shortest paths
• Graphs with negative edges
• Graphs with cycles
– Can a shortest path contain a cycle?
Are shortest paths unique?
Are shortest paths unique?
Are shortest paths unique?
Shortest Path Algorithms

v.d is a shortest path estimate to v from s


Relaxing

The process of relaxing an edge (u,v) consists of


testing whether we can improve the shortest path to
v found so far by going through u and, if so, updating
v.d and v.π.
Relaxing
Properties of shortest paths and relaxation
Properties of shortest paths and relaxation
Bellman Ford Algorithm
Bellman Ford Example
Bellman Ford Example
Bellman Ford Example
Bellman Ford Example
Bellman Ford Example
shortest path in DAG
Dijkstra’s algorithm
Dijkstra’s algorithm

•Can be used only when all weights are


non-negative
•It maintains a set S of vertices whose final
shortest-path weights from the source s
have already been determined.
•Algorithm uses a greedy strategy. It
chooses the “lightest” or “closest” vertex
in V - S
Dijkstra’s algorithm - Example
Dijkstra’s algorithm - Example
Dijkstra’s algorithm - Example
Dijkstra’s algorithm - Example
Dijkstra’s algorithm - Example
Dijkstra’s algorithm - Example
The Floyd-Warshall algorithm
• The Floyd-Warshall algorithm considers the
intermediate vertices of a shortest path,
where an intermediate vertex of a simple path
p = <v1,v2,…vl> is any vertex of p other than
v1 or vl
The Floyd-Warshall algorithm
The Floyd-Warshall algorithm
Printing Shortest Path
Filling predecessor Matrix
Transitive closure of a graph
We define the transitive closure of G as the
graph G* = (V,E*), where E* = { (i,j): there is a
path from vertex i to vertex j in G}

One way to compute the transitive closure of a


graph is to assign a weight of 1 to each edge
of E and run the Floyd-Warshall algorithm.
Transitive closure Algorithm
Transitive closure Algorithm
Flow Networks
• G = (V,E) directed.
• Each edge (u,v) has a capacity c(u,v) >= 0
• If (u,v) not є E, then c(u,v) = 0.
• If (u,v) є E, then reverse edge (v,u) not є E.
• Source vertex s, sink vertex t , assume s  v
 t for all v є V , so that each vertex lies on a
path from source to sink.
Flow Network
Another Flow Network
Flow
• A function f: V x V -> R satisfying
– Capacity constraint
For all u,v є V, 0 <= f(u,v) <= c(u,v)
– Flow Constraint
Flows
Another Flow
Multiple sources
Maximum Flow Problem
• The value |f| of a flow f is defined as

• In the maximum-flow problem, we are given a


flow network G with source s and sink t , and
we wish to find a flow of maximum value.
Network with anti parallel edges
Converted graph
The Ford-Fulkerson method
• FORD-FULKERSON-METHOD (G, s, t)
• 1 initialize flow f to 0
• 2 while there exists an augmenting path p in
the residual network Gf
• 3 augment flow f along p
• 4 return f
Residual network
Residual network

Given a flow network G = (V,E) and a flow f ,


the residual network of G induced by f is Gf
=(V, Ef), where
Residual network
Augmenting flow
• If f is a flow in G and f’ is a flow in the
corresponding residual network Gf , we define
f ↑ f’, the augmentation of flow f by f’, to be
a function from V x V to R, defined by
Augmenting paths
• An augmenting path p is a simple path from s
to t in the residual network Gf
• Residual Capacity of path p
– The maximum amount by which we can increase
the flow on each edge in an augmenting path p

cf(p) = min { cf(u,v): (u,v) is on p}


Updated Flow Network
Cuts of flow networks
• A cut (S,T) of flow network G = (V,E) is a partition of V
into S and T = V - S such that s є S and t є T .
• If f is a flow, then the net flow f(S,T) across the cut
(S,T) is defined to be

•Capacity of cut (S,T) is


Example of a cut
Flows through a cut
• Lemma: Let f be a flow in a flow network G
with source s and sink t, and let (S,T) be any
cut of G. Then the net flow across (S,T) is f(S,T)
= |f|
• Corollary
– The value of any flow f in a flow network G is
bounded from above by the capacity of any cut of
G.
Max-flow Min-cut Theorem
• If f is a flow in a flow network G = (V,E) with
source s and sink t , then the following
conditions are equivalent:
– 1. f is a maximum flow in G.
– 2. The residual network Gf contains no augmenting
paths.
– 3|f| = c(S,T) for some cut (S,T) of G.
Ford Fulkerson Algorithm
Ford Fulkerson - Example
Ford Fulkerson - Example
Ford Fulkerson - Example
Ford Fulkerson - Example
Ford Fulkerson - Example
Ford Fulkerson - Example
Ford Fulkerson - Example
Ford Fulkerson - Example
Ford Fulkerson - Example
Ford Fulkerson - Example
Ford Fulkerson - Example
Ford Fulkerson - Complexity
• O( E. |f|)
Maximum bipartite matching
Given a graph G=(V,E)
– A matching is a subset of edges M of E such that
for all vertices v in V, at most one edge of M is
incident on v.
– A maximum matching is a matching of maximum
cardinality
Maximum bipartite matching
Maximum bipartite matching
Maximum bipartite matching

You might also like