Download presentation
Presentation is loading. Please wait.
Published byLeslie Norman Modified over 5 years ago
1
Text Book: Introduction to algorithms By C L R S
Graph Algorithms Text Book: Introduction to algorithms By C L R S
2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
Representation of graphs
9
Representation of graphs
10
Representation of graphs
11
Representation of graphs
12
BFS
13
BFS
14
BFS Example
15
BFS Example
16
BFS Example
17
Properties of BFS Running time O(V + E)
BFS computes shorted path distance Algorithm BFS builds a breadth first tree.
18
Printing shortest path
19
DFS
20
DFS
21
DFS Example
22
DFS Example
23
DFS Example
24
DFS Example
25
DFS Example
26
DFS – Example 2
27
DFS – Example 2
28
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.
29
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.
30
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.
31
Topological sort
32
Topological sort - algorithm
33
Topological sort
34
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.
35
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
36
Strongly connected components- Example
37
Strongly connected components- Example
38
Component Graph
39
Minimal Spanning tree
40
Minimal Spanning tree
41
Kruskal’s Algorithm Example
42
Kruskal’s Algorithm Example
43
Kruskal’s Algorithm Example
44
Kruskal’s Algorithm Example
45
Kruskal’s Algorithm Example
46
Kruskal’s Algorithm Example
47
Kruskal’s Algorithm Example
48
Kruskal’s Algorithm Example
49
Kruskal’s Algorithm Example
50
Kruskal’s Algorithm Example
51
Kruskal’s Algorithm Example
52
Kruskal’s Algorithm Example
53
Kruskal’s Algorithm Example
54
Kruskal’s Algorithm Example
55
Kruskal’s Algorithm Running time O(E lg V)
56
Prim’s Algorithm
57
Prim’s Algorithm - Example
58
Prim’s Algorithm - Example
59
Prim’s Algorithm - Example
60
Prim’s Algorithm - Example
61
Prim’s Algorithm - Example
62
Prim’s Algorithm - Example
63
Prim’s Algorithm - Example
64
Prim’s Algorithm - Example
65
Prim’s Algorithm - Example
66
Generic MST Algorithm
67
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.
68
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.
69
Cut - Example
70
Cut – Another View
71
Shortest Path The weight w(p) of path p = <v0, v1, , vk> is the sum of the weights of its constituent edges:
72
Shortest Path Problems
single-source shortest-paths problem Single-destination shortest-paths problem Single-pair shortest-path problem All-pairs shortest-paths problem
73
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?
74
Are shortest paths unique?
75
Are shortest paths unique?
76
Are shortest paths unique?
77
Shortest Path Algorithms
v.d is a shortest path estimate to v from s
78
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.π.
79
Relaxing
80
Properties of shortest paths and relaxation
81
Properties of shortest paths and relaxation
82
Bellman Ford Algorithm
83
Bellman Ford Example
84
Bellman Ford Example
85
Bellman Ford Example
86
Bellman Ford Example
87
Bellman Ford Example
88
shortest path in DAG
89
Dijkstra’s algorithm
90
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
91
Dijkstra’s algorithm - Example
92
Dijkstra’s algorithm - Example
93
Dijkstra’s algorithm - Example
94
Dijkstra’s algorithm - Example
95
Dijkstra’s algorithm - Example
96
Dijkstra’s algorithm - Example
97
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
98
The Floyd-Warshall algorithm
99
The Floyd-Warshall algorithm
100
Printing Shortest Path
101
Filling predecessor Matrix
102
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.
103
Transitive closure Algorithm
104
Transitive closure Algorithm
105
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.
106
Flow Network
107
Another Flow Network
108
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
109
Flows
110
Another Flow
111
Multiple sources
112
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.
113
Network with anti parallel edges
114
Converted graph
115
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
116
Residual network
117
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
118
Residual network
119
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
120
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}
121
Updated Flow Network
122
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
123
Example of a cut
124
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.
125
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.
126
Ford Fulkerson Algorithm
127
Ford Fulkerson - Example
128
Ford Fulkerson - Example
129
Ford Fulkerson - Example
130
Ford Fulkerson - Example
131
Ford Fulkerson - Example
132
Ford Fulkerson - Example
133
Ford Fulkerson - Example
134
Ford Fulkerson - Example
135
Ford Fulkerson - Example
136
Ford Fulkerson - Example
137
Ford Fulkerson - Example
138
Ford Fulkerson - Complexity
O( E. |f|)
139
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
140
Maximum bipartite matching
141
Maximum bipartite matching
142
Maximum bipartite matching
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.