11 g64ads advanced data structures graph algorithms
TRANSCRIPT
11
G64ADSAdvanced Data Structures
Graph Algorithms
22
Going from A to B hellip
o What data structure to use
o How to think about the problem
A B C
D E
G
F
H
33
Going from A to B hellip
o Strip away the irrelevant details hellip
o Vertices
A B C
D E
G
F
H
44
Going from A to B hellip
o Strip away the irrelevant details hellip
o Edges
A B C
D E
G
F
H
55
Going from A to B hellip
o Strip away the irrelevant details hellip
o Weights
A B C
D E
G
F
H
3 4
34
4 56
4
66
Going from A to B hellip
o Strip away the irrelevant details hellip
o Now much simpler hellip
A B C
D E
G
F
H
3 4
34
4 56
4
7
Graphs
o Are a tool for modeling real world problems
o Allow us to abstract details and focus on the problem
o We can represent our domain as graphs apply algorithms to solve our problem
8
Simple Graphs
9
Directed Graphs
10
Weighted Graphs
3 4
34
4 56
4
11
Path and Cycle
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
22
Going from A to B hellip
o What data structure to use
o How to think about the problem
A B C
D E
G
F
H
33
Going from A to B hellip
o Strip away the irrelevant details hellip
o Vertices
A B C
D E
G
F
H
44
Going from A to B hellip
o Strip away the irrelevant details hellip
o Edges
A B C
D E
G
F
H
55
Going from A to B hellip
o Strip away the irrelevant details hellip
o Weights
A B C
D E
G
F
H
3 4
34
4 56
4
66
Going from A to B hellip
o Strip away the irrelevant details hellip
o Now much simpler hellip
A B C
D E
G
F
H
3 4
34
4 56
4
7
Graphs
o Are a tool for modeling real world problems
o Allow us to abstract details and focus on the problem
o We can represent our domain as graphs apply algorithms to solve our problem
8
Simple Graphs
9
Directed Graphs
10
Weighted Graphs
3 4
34
4 56
4
11
Path and Cycle
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
33
Going from A to B hellip
o Strip away the irrelevant details hellip
o Vertices
A B C
D E
G
F
H
44
Going from A to B hellip
o Strip away the irrelevant details hellip
o Edges
A B C
D E
G
F
H
55
Going from A to B hellip
o Strip away the irrelevant details hellip
o Weights
A B C
D E
G
F
H
3 4
34
4 56
4
66
Going from A to B hellip
o Strip away the irrelevant details hellip
o Now much simpler hellip
A B C
D E
G
F
H
3 4
34
4 56
4
7
Graphs
o Are a tool for modeling real world problems
o Allow us to abstract details and focus on the problem
o We can represent our domain as graphs apply algorithms to solve our problem
8
Simple Graphs
9
Directed Graphs
10
Weighted Graphs
3 4
34
4 56
4
11
Path and Cycle
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
44
Going from A to B hellip
o Strip away the irrelevant details hellip
o Edges
A B C
D E
G
F
H
55
Going from A to B hellip
o Strip away the irrelevant details hellip
o Weights
A B C
D E
G
F
H
3 4
34
4 56
4
66
Going from A to B hellip
o Strip away the irrelevant details hellip
o Now much simpler hellip
A B C
D E
G
F
H
3 4
34
4 56
4
7
Graphs
o Are a tool for modeling real world problems
o Allow us to abstract details and focus on the problem
o We can represent our domain as graphs apply algorithms to solve our problem
8
Simple Graphs
9
Directed Graphs
10
Weighted Graphs
3 4
34
4 56
4
11
Path and Cycle
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
55
Going from A to B hellip
o Strip away the irrelevant details hellip
o Weights
A B C
D E
G
F
H
3 4
34
4 56
4
66
Going from A to B hellip
o Strip away the irrelevant details hellip
o Now much simpler hellip
A B C
D E
G
F
H
3 4
34
4 56
4
7
Graphs
o Are a tool for modeling real world problems
o Allow us to abstract details and focus on the problem
o We can represent our domain as graphs apply algorithms to solve our problem
8
Simple Graphs
9
Directed Graphs
10
Weighted Graphs
3 4
34
4 56
4
11
Path and Cycle
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
66
Going from A to B hellip
o Strip away the irrelevant details hellip
o Now much simpler hellip
A B C
D E
G
F
H
3 4
34
4 56
4
7
Graphs
o Are a tool for modeling real world problems
o Allow us to abstract details and focus on the problem
o We can represent our domain as graphs apply algorithms to solve our problem
8
Simple Graphs
9
Directed Graphs
10
Weighted Graphs
3 4
34
4 56
4
11
Path and Cycle
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
7
Graphs
o Are a tool for modeling real world problems
o Allow us to abstract details and focus on the problem
o We can represent our domain as graphs apply algorithms to solve our problem
8
Simple Graphs
9
Directed Graphs
10
Weighted Graphs
3 4
34
4 56
4
11
Path and Cycle
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
8
Simple Graphs
9
Directed Graphs
10
Weighted Graphs
3 4
34
4 56
4
11
Path and Cycle
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
9
Directed Graphs
10
Weighted Graphs
3 4
34
4 56
4
11
Path and Cycle
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
10
Weighted Graphs
3 4
34
4 56
4
11
Path and Cycle
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
11
Path and Cycle
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
12
Representation of Graphs
o Adjacency matrix
o Adjacency list
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
13
Representation of Graphs
o Adjacency list
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
14
Topological Sort
o Ordering of vertices in a directed acyclic graph such that if there is a path from vi to vj the vj appears after vi in the ordering
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
15
Topological Sort
o Applicationo Given a number of tasks there are often a
number of constraints between the taskso task A must be completed before task B can
start
o These tasks together with the constraints form a directed acyclic graph
o A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
16
Topological Sort
o Applicationo Consider someone getting ready to go
out for dinneroHe must wear the following
o jacket shirt briefs socks tie Blackberry etc
oThere are certain constraintso the pants really should go on after the briefso the Blackberry must be clipped on the belto socks are put on before shoes
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
17
Topological Sort
o Applicationo Getting dress graph
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
18
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
19
Topological Sort
o Applicationo Getting dress graph
One topological sort is
briefs pants wallet keys belt Blackberry socks shoes shirt tie jacket iPod watch
another possible solution
briefs socks pants shirt belt tie jacket
wallet keys Blackberry iPod watch shoes
Not unique
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
20
Topological Sort
o Course prerequisite structure represented in an acyclic graph
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
21
Topological Sort
o Topological Sort (Relabel) Given a directed acyclic graph (DAG) relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one
5
4
6
2 8
3
1
7
1
2
4
5 3
6
7
8
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
22
Topological Sort
o Topological Sort (rearrange) Given a directed acyclic graph (DAG) rearrange its vertices on a horizontal line such that all the directed edges points from left to right
6 45 2 8 317
1
2
4
5 3
6
7
8
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
23
webmitedujorlinwww15082AnimationsTopological_Sortingppt
Following slides are taken from
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
24
Initialization1
2
4
5 3
6
7
8
next 0
1 2 3 4 5 6 7 8
2 2 3 2 1 1 0 2
Node
Indegree
Determine the indegree of each node
LIST is the set of nodes with indegree of 0
ldquoNextrdquo will be the label of nodes in the topological order
LIST
7
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
25
Select a node from LIST
next 0
1 2 3 4 5 7 8
2 2 3 2 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
015
6
1
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
26
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
27
Select a node from LIST
next 0
1 2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
01
2
3
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
28
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
29
Select a node from LIST
next 0
2 3 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
4
6
6
3
0
2
2
1
1
4
0
1
3
4
1
5
5
2 1
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
30
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
7
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0 5
5
2
1
6
0
4
210
2
46
6
3
0 2
2
1
1
4
0 1
3
4
1
5
5
1
4
3
2
6
01
6
8
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
31
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
83
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
32
Select a node from LIST
next 0
2 5 7 8
2 2 3 1 1 0 2
Node
Indegree
Select a node from LIST and delete it
LIST
next = next +1order(i) = next
update indegrees
update LIST1
1
1
2
4
5 3
6
7
8
7
0
5
2
1
6
0
4
210
2
6
3
0
2
1
1
4
0
3
4
1
5
5
1
4
3
2
6
01
6
8
7
7
0
3
8
8
List is empty
The algorithm terminates with a topological order of the nodes
3
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
33
Example
o Consider the following DAG with six vertices
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
34
Example
o Let us define the table of in-degrees
1 0
2 1
3 3
4 3
5 2
6 0
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
35
Example
o And a queue into which we can insert vertices 1 and 6
1 0
2 1
3 3
4 3
5 2
6 01 6Queue
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
36
Example
o We dequeue the head (1) decrement the in-degree of all adjacent vertices and enqueue 2
1 0
2 0
3 3
4 2
5 2
6 06 2Queue
Sort1
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
37
Example
o We dequeue 6 and decrement the in-degree of all adjacent vertices
1 0
2 0
3 2
4 2
5 1
6 02Queue
Sort1 6
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
38
Example
o We dequeue 2 decrement and enqueue vertex 5
1 0
2 0
3 1
4 1
5 0
6 05Queue
Sort1 6 2
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
39
Example
o We dequeue 5 decrement and enqueue vertex 3
1 0
2 0
3 0
4 1
5 0
6 03Queue
Sort1 6 2 5
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
40
Example
o We dequeue 3 decrement 4 and add 4 to the queue
1 0
2 0
3 0
4 0
5 0
6 04Queue
Sort1 6 2 5 3
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
41
Example
o We dequeue 4 there are no adjacent vertices to decrement the in degree
1 0
2 0
3 0
4 0
5 0
6 0Queue
Sort1 6 2 5 3 4
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
42
Example
o The queue is now empty so a topological sort is 1 6 2 5 3 4
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
43
Topological Sort
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
44
Topological Sort
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
45
Shortest-Path Algorithm
o Find the ldquoshortestrdquo path from point A to point Bo 1048708ldquoShortestrdquo in time distance cost hellipo 1048708Numerous applications
o Map navigationo Flight itinerarieso Circuit wiring
o Network routing
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
46
Shortest Path Problems
o Input is a weighted graph where each edge (vivj) has cost cij to traverse the edge
o 1048708Cost of a path v1v2hellipvN
o Unweighted path length is N-1 number of edges on path
1
11
N
iiic
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
47
Shortest Path Problems
o Single-source shortest path problemo Given a weighted graph G=(VE) and a start
vertex s find the minimum weighted path from s to every other vertex in G
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
48
Negative Weights
o Graphs can have negative weightso eg arbitrage
o Shortest positive-weight path is a net gaino Path may include individual losses
o Problem Negative weight cycleso Allow arbitrarily-low path costs
o Solutiono Detect presence of negative-weight cycles
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
49
Shortest Path Problems
o Unweighted shortest-path problem O(|E|+|V|)
o Weighted shortest-path problemo No negative edges O(|E| log |V|)o Negative edges O(|E|middot|V|)
o Acyclic graphs O(|E|+|V|)o No asymptotically faster algorithm for single-
sourcesingle-destination shortest path problem
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
50
Unweighted Shortest Paths
o No weights on edgeso Find shortest length pathso Same as weighted shortest path with all weights
equalo Breadth-first search
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
51
Unweighted Shortest Paths
o For each vertex keep track ofo Whether we have visited it (known)
o Its distance from the start vertex (dv)
o Its predecessor vertex along the shortest path from the start vertex (pv)
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
52
Unweighted Shortest PathsSolution 1 Repeatedly iterate through vertices looking for unvisited vertices at current distance from start vertex s
Running time O(|V|2)
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
53
Unweighted Shortest Paths
Solution 2 Ignore vertices that have already been visited by keeping only unvisited vertices (distance = infin) on the queueRunning time O(|E|+|V|)
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
54
Unweighted Shortest Paths
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
55
Weighted Shortest Paths
o Dijkstrarsquos algorithm
o Use priority queue to store unvisited vertices by distance from s
o After deleteMin v update distances of remaining vertices adjacent to v using decreaseKey
o Does not work with negative weights
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
56
Weighted Shortest Paths
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
57
Weighted Shortest Paths
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
58
Weighted Shortest Paths
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
59
Weighted Shortest Paths
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
60
Why Dijkstra Works
o Hypothesiso A least-cost path from X to Y contains least-cost paths
from X to every city on the path
o eg if XrarrC1rarrC2rarrC3rarrY is the least-cost path from X to Y then
o XrarrC1rarrC2rarrC3 is the least-cost path from X to C3o XC1rarrC2 is the least-cost path from X to C2o XrarrC1 is the least-cost path from X to C1
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
61
Why Dijkstra Works
o Assume hypothesis is false ie Given a least-cost path P from X to Y that goes through C there is a better path Prsquo from X to C than the one in P
o Show a contradictiono But we could replace the subpath from X to C in P with this
lesser-cost path Prsquoo The path cost from C to Y is the sameo Thus we now have a better path from X to Yo But this violates the assumption that P is the least-cost path
from X to Y
o Therefore the original hypothesis must be true
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
62
Network Flow
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
63
Network Flow Approach
o Let the waypoints (cities distribution centers) be vertices in a graph
o Let the routes (roads rails waterways airways) between waypoints be directed edges in the graph
o Each edge has a weight representing the routersquos capacity
o Choose a starting point s and an ending point t
o Determine the maximum rate (eg tonshour) at which we can flow freight from point s to point to Without the freight piling up anywhere along the way
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
64
Network Flow Application
o Freight flow through shipping networks
o Fluid flow through a network of pipes
o Data flow (bandwidth) through computer networks
o Nutrient flow through food networks
o Electricity flow through power distribution systems
o People flow through mass transportation systems
o Traffic flow through a city
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
65
Network Flow Problems
o Giveno Directed graph G=(VE) with edge capacities cvwo Source vertex so Sink vertex t
o Constraintso Flow along directed edge (vw) cannot exceed capacity cvwo At every vertex (except s and t) the total flow coming in must
equal the total flow going out
o FindMaximum amount of flow from s to t
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
66
Network Flow
o Example network graph (left) and its maximum flow (right)
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
67
Maximum Flow Algorithm
o Flow graph Gf
o Indicates the amount of flow on each edge in the network
o Residual graph Gr
o Indicates how much more flow can be added to each edge in the network
o Residual capacity = (capacity ndash current flow)o Edges with zero residual capacity removed
o Augmenting patho Path from s to t in Gr
o Edge with smallest residual capacity in the path indicates amount by which flow can increase along path
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
68
Maximum Flow Algorithm
Initial stage flow graph and residual graph
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
69
Maximum Flow Algorithm
Initial stage flow graph and residual graph
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
70
Maximum Flow Algorithm
o While an augmenting path exists in Gro Choose oneo Flow increase FI = minimum residual capacity
along augmenting patho Increase flows along augmenting path in flow
graph Gf by FIo Update residual graph Gr
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
71
Maximum Flow Algorithm
Example (cont) after choosing (sbdt)
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
72
Maximum Flow Algorithm
Example (cont) after choosing (sact)
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
73
Maximum Flow Algorithm
Example (cont) after choosing (sadt)
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
74
Maximum Flow Algorithm
o Problem Suppose we chose augmenting path (sadt) first
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
75
Maximum Flow Algorithm
o Solutiono Indicate potential for back flow in residual
grapho ie allow another augmenting path to undo
some of the flow used by a previous augmenting path
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
76
Maximum Flow Algorithm
o Example (cont) after choosing (sbdact)
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
77
Maximum Flow Algorithm
Analysis
o If edge capacities are rational numbers then this algorithm always terminates with maximum flow
o If capacities are integers and maximum flow is f then running time is O(f middot |E|)
o Flow always increases by at least 1 with each augmenting path
o Augmenting path can be found on O(|E|) time using unweighted shortest-path algorithm
o Problem Can be slow for large f
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
78
Maximum Flow Algorithm
o Variants
o Always choose augmenting path allowing largest increase in flow
o O(|E|) calls to O(|E| log |V|) Dijkstra algorithmo Running time O(|E|2log |V|)
o Always choose the augmenting path with the fewest edges (unweighted shortest path)
o At most O(|E||V|) augmenting steps each costing O(|E|) for call to BFS
o Running time O((|E|2|V|)
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
79
Maximum Flow Algorithm
o Variants
o Multiple sources and sinkso Create super-source with infinite-capacity links to each
sourceo Create super-sink with infinite-capacity links from each
sink
o Min-cost flowo Each edge has capacity and costo Find maximum flow with minimum costo No known fast solution
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
80
Minimum Spanning Trees
o Find a minimum-cost set of edges that connect all vertices of a graph
o Applicationso Connecting ldquonodesrdquo with a minimum of ldquowirerdquo
o Networkingo Circuit design
o Collecting nearby nodeso Clustering taxonomy construction
o Approximating graphso Most graph algorithms are faster on trees
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
81
Minimum Spanning Trees
o A tree is an acyclic undirected connected graph
o A spanning tree of a graph is a tree containing all vertices from the graph
o A minimum spanning tree is a spanning tree where the sum of the weights on the treersquos edges are minimal
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
82
Minimum Spanning Trees
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
83
Minimum Spanning Trees
o Problemo Given an undirected weighted graph G=(VE)
with weights w(u v) for each (uv) isin E
o Find an acyclic connected graph Grsquo=(VErsquo) Ersquo subeE that minimizes Σ(uv) isin Ersquo w(u v)
o Grsquo is a minimum spanning treeo There can be more than one
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
84
Minimum Spanning Trees
o Solution 1
o Start with an empty tree To While T is not a spanning tree
o Find the lowest-weight edge that connects a vertex in T to a vertex not in T
o Add this edge to T
o T will be a minimum spanning tree
o Called Primrsquos algorithm (1957)
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
85
Primrsquos Algorithm Example
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
86
Primrsquos Algorithm
o Similar to Dijkstrarsquos shortest-path algorithm
o Except
o vknown = v in To vdist = weight of lowest-weight edge connecting v to
a known vertex in To vpath = last neighboring vertex changing (lowering)
vrsquos dist value (same as before)
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
87
Primrsquos Algorithm
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
88
Primrsquos Algorithm
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
89
Minimum Spanning Tree
o Solution 2o Start with T = V (no edges)o For each edge in increasing order by weight
o If adding edge to T does not create a cycleo Then add edge to T
o T will be a minimum spanning treeo Called Kruskalrsquos algorithm (1956)
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
90
Kruskalrsquos Algorithm Example
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
91
Kruskalrsquos Algorithm
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
92
Kruskalrsquos Algorithm Analysis
o Worst case O(|E| log |E|)
o Since |E| = O(|V|2) worst case also O(|E| log |V|)
o Running time dominated by heap operations
o Typically terminates before considering all edges so faster in practice
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
93
Minimum Spanning Tree Applications
o Feature extraction from remote sensing images (eg roads rivers etc)o Cosmological structure formation
o Cancer imaging Arrangement of cells in the epithelium (tissue surrounding organs)
o Approximate solution to traveling salesman problemo Most of above use Euclidian MSTIe weights are
Euclidean distances between vertices
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
94
Applications
o Depth-first searcho Biconnectivityo Euler circuitso Strongly-connected components
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
95
Depth-First Search
o Recursively visit every vertex in the graph
o Considers every edge in the grapho Assumes undirected edge (uv) is in ursquos and
vrsquos adjacency list
o Visited flag prevents infinite loops
o Running time O(|V|+|E|)
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
96
Depth-First Search
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
97
Biconnectivity
o A connected undirected graph is biconnected if the graph is still connected after removing any one vertexo ie when a ldquonoderdquo fails there is always an
alternative route
o If a graph is not biconnected the disconnecting vertices are called articulation points
oCritical points of interest in many applications
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
98
Biconnectivity
BiconnectedArticulation points
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
99
Biconnectivity
o Finding Articulation Points
o From any vertex v perform DFS and number vertices as they are visited
o Num(v) is the visit number
o Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
o Low(v) = minimum of Num(v)o Lowest Num(w) among all back edges (vw)o Lowest Low(w) among all tree edges (vw)
o Can compute Num(v) and Low(v) in O(|E|+|V|) time
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
100
Biconnectivity
o Finding Articulation Points
Depth-first tree starting at A with NumLow values
Original Graph
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
101
Biconnectivity
o Finding Articulation Points
o Root is articulation point iff it has more than one child
o Any other vertex v is an articulation point iff v has some child w such that Low(w) ge Num(v)o ie is there a child w of v that cannot reach
a vertex visited before vo If yes then removing v will disconnect w
(and v is an articulation point)
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
102
Biconnectivity
o Finding Articulation Points
Original Graph
Depth-first tree starting at C with NumLow values
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
103
Biconnectivity
o Finding Articulation Points
o High-level algorithmo Perform pre-order traversal to compute Numo Perform post-order traversal to compute Lowo Perform another post-order traversal to detect
articulation points
o Last two post-order traversals can be combined
o In fact all three traversals can be combined in one recursive algorithm
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
104
Biconnectivity
o Finding Articulation Points
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
105
Biconnectivity
o Finding Articulation Points
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
106
Biconnectivity
o Finding Articulation Points
Check for root omitted
Test for articulation points in one depth-first search
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
107
Euler Circuits
Puzzle challengeo Can you draw a figure using a pen drawing
each line exactly once without lifting the pen from the paper
o And can you finish where you started
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
108
Euler Circuits
o Solved by Leonhard Euler in 1736 using a graph approach (DFS)o Also called an ldquoEuler pathrdquo or ldquoEuler tourrdquoo Marked the beginning of graph theory
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
109
Euler Circuit Problem
o Assign a vertex to each intersection in the drawingo Add an undirected edge for each line segment in the
drawingo Find a path in the graph that traverses each edge
exactly once and stops where it started
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
110
Euler Circuit Problem
o Necessary and sufficient conditionso Graph must be connectedo Each vertex must have an even degree
o Graph with two odd-degree vertices can have an Euler tour (not circuit)
o Any other graph has no Euler tour or circuit
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
111
Euler Circuit Problem
o Algorithm
o Perform DFS from some vertex v until you return to v along path p
o If some part of graph not included perform DFS from first vertex vrsquo on p that has an un-traversed edge (path prsquo)
o Splice prsquo into po Continue until all edges traversed
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
112
Euler Circuit Example
Start at vertex 5Suppose DFS visits 5 4 10 5
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
113
Euler Circuit ExampleGraph remaining after 5 4 10 5
Start at vertex 4Suppose DFS visits 4 1 3 7 4 11 10 7 9 3 4
Splicing into previous path 5 4 1 3 7 4 11 10 7 9 3 4 10 5
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
114
Euler Circuit Example
Graph remaining after 5 4 1 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 3Suppose DFS visits 3 2 8 9 6 3Splicing into previous path 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
115
Euler Circuit Example
Graph remaining after 5 4 1 3 2 8 9 6 3 7 4 11 10 7 9 3 4 10 5
Start at vertex 9Suppose DFS visits 9 12 10 9
Splicing into previous path 5 4 1 3 2 8 9 12 10 9 6 3 7 4 11 10 7 9 3 4 10 5
No more un-traversed edges so above path is an Euler circuit
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
116
Euler Circuit Algorithm
o Implementation detailso Maintain circuit as a linked list to support O(1) splicingo Maintain index on adjacency lists to avoid repeated
searches for un-traversed edges
o Analysiso Each edge considered only onceo Running time is O(|E|+|V|)
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
117
Strongly-Connected Components
o A graph is strongly connectedif every vertex can be reached from every other vertexo A strongly-connected componentof a graph is a
subgraph that is strongly connectedo Would like to detect if a graph is strongly connectedo Would like to identify strongly-connected components
of a grapho Can be used to identify weaknesses in a networko General approach Perform two DFSs
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
118
Strongly-Connected Components
o Algorithmo Perform DFS on graph G
o Number vertices according to a post-order traversal of the DF spanning forest
o Construct graph Grby reversing all edges in G
o Perform DFS on Gro Always start a new DFS (initial call to Visit) at the highest-
numbered vertex
o Each tree in resulting DF spanning forest is a strongly-connected component
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
119
Strongly-Connected Components
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
120
Strongly-Connected Components
o Correctnesso If v and w are in a strongly-connected componento Then there is a path from v to w and a path from w to vo Therefore there will also be a path between v and w in G and Gr
o Running timeo Two executions of DFSo O(|E|+|V|)
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-
121
Summary
o Graphs one of the most important data structureso Studied for centurieso Numerous applicationso Some of the hardest problems to solve are graph
problems eg Hamiltonian (simple) cycle Clique
- G64ADS Advanced Data Structures
- Going from A to B hellip
- Slide 3
- Slide 4
- Slide 5
- Slide 6
- Graphs
- Simple Graphs
- Directed Graphs
- Weighted Graphs
- Path and Cycle
- Representation of Graphs
- Slide 13
- Topological Sort
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Slide 19
- Slide 20
- Slide 21
- Slide 22
- webmitedujorlinwww15082AnimationsTopological_Sortingppt
- Initialization
- Select a node from LIST
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Example
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Shortest-Path Algorithm
- Shortest Path Problems
- Slide 47
- Negative Weights
- Slide 49
- Unweighted Shortest Paths
- Slide 51
- Slide 52
- Slide 53
- Slide 54
- Weighted Shortest Paths
- Slide 56
- Slide 57
- Slide 58
- Slide 59
- Why Dijkstra Works
- Slide 61
- Network Flow
- Network Flow Approach
- Network Flow Application
- Network Flow Problems
- Slide 66
- Maximum Flow Algorithm
- Slide 68
- Slide 69
- Slide 70
- Slide 71
- Slide 72
- Slide 73
- Slide 74
- Slide 75
- Slide 76
- Slide 77
- Slide 78
- Slide 79
- Minimum Spanning Trees
- Slide 81
- Slide 82
- Slide 83
- Slide 84
- Primrsquos Algorithm Example
- Primrsquos Algorithm
- Slide 87
- Slide 88
- Minimum Spanning Tree
- Kruskalrsquos Algorithm Example
- Kruskalrsquos Algorithm
- Kruskalrsquos Algorithm Analysis
- Minimum Spanning Tree Applications
- Applications
- Depth-First Search
- Slide 96
- Biconnectivity
- Slide 98
- Slide 99
- Slide 100
- Slide 101
- Slide 102
- Slide 103
- Slide 104
- Slide 105
- Slide 106
- Euler Circuits
- Slide 108
- Euler Circuit Problem
- Slide 110
- Slide 111
- Euler Circuit Example
- Slide 113
- Slide 114
- Slide 115
- Euler Circuit Algorithm
- Strongly-Connected Components
- Slide 118
- Slide 119
- Slide 120
- Summary
-