incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud.. It adds vertices by increasing distance..[r]
Trang 1Data Structures and
Algorithms
"
Graphs!
Trang 4• A graph is a pair (V, E) , where!
– V is a set of nodes, called vertices!
– E is a collection of pairs of vertices, called edges!
– Vertices and edges are positions and store elements!
• Example:!
– A vertex represents an airport and stores the three-letter airport code! – An edge represents a flight route between two airports and stores the mileage of the route!
MIA DFW
SFO
LAX
LGA HNL
Trang 5Phạm Bảo Sơn - DSA 5
Edge Types"
• Directed edge!
– ordered pair of vertices ( u , v )
– first vertex u is the origin!
– second vertex v is the destination!
– e.g., a flight!
• Undirected edge!
– unordered pair of vertices ( u , v )
– e.g., a flight route
• Directed graph!
– all the edges are directed!
– e.g., route network!
• Undirected graph!
– all the edges are undirected!
– e.g., flight network!
ORD flight AA 1206 PVD
Trang 6John David
Trang 7Phạm Bảo Sơn - DSA 7
Terminology"
• End vertices (or endpoints) of
an edge!
– U and V are the endpoints of a!
• Edges incident on a vertex!
– a, d, and b are incident on V!
Trang 8P 1
Terminology (cont.)"
• Path!
– sequence of alternating
vertices and edges !
– begins with a vertex!
– ends with a vertex!
– each edge is preceded and
followed by its endpoints!
• Simple path!
– path such that all its vertices
and edges are distinct!
Trang 9Phạm Bảo Sơn - DSA 9
Terminology (cont.)"
• Cycle!
– circular sequence of alternating
vertices and edges !
– each edge is preceded and
followed by its endpoints!
• Simple cycle!
– cycle such that all its vertices
and edges are distinct!
Trang 11Phạm Bảo Sơn - DSA 11
Main Methods of the Graph ADT"
• Vertices and edges!
– are positions!
– store elements!
• Accessor methods!
– endVertices(e): an array of
the two endvertices of e!
– opposite(v, e): the vertex
opposite of v on e!
– areAdjacent(v, w): true iff v
and w are adjacent!
an edge (v,w) storing element o!
– removeVertex(v): remove vertex v (and its incident edges)!
– removeEdge(e): remove edge e!
• Iterator methods!
– incidentEdges(v): edges incident to v!
– vertices(): all vertices in the graph!
– edges(): all edges in the graph!
Trang 12Data Structures for Graphs"
• Edge List!
• Adjacency List!
• Adjacency Matrix!
Trang 13Phạm Bảo Sơn - DSA 13
– origin vertex object!
– destination vertex object!
– reference to position in edge
Trang 14Adjacency List Structure "
• Edge list structure!
Trang 15Phạm Bảo Sơn - DSA 15
• Edge list structure!
• Augmented vertex
objects!
– Integer key (index)
associated with vertex!
• The “ old fashioned ”
version just has 0 for no
edge and 1 for edge!
Trang 18• A subgraph S of a graph
G is a graph such that !
– The vertices of S are a
subset of the vertices of G!
– The edges of S are a
subset of the edges of G!
Trang 19Phạm Bảo Sơn - DSA 19
Trang 20Trees and Forests"
This definition of tree is
different from the one of a
Trang 21Phạm Bảo Sơn - DSA 21
Spanning Trees and Forests"
• A spanning tree of a
connected graph is a
spanning subgraph that is
a tree!
• A spanning tree is not
unique unless the graph is
a tree!
• Spanning trees have
applications to the design
Trang 22– Find and report a path between two given vertices!
– Find a cycle in the graph!
• Depth-first search is to graphs what Euler tour
is to binary trees!
Trang 23Phạm Bảo Sơn - DSA 23
DFS Algorithm"
• The algorithm uses a mechanism for
setting and getting “ labels ” of
Input graph G and a start vertex v of G
Output labeling of the edges of G
in the connected component of v
as discovery edges and back edges
else setLabel(e, BACK)
Algorithm DFS(G)
Input graph G
Output labeling of the edges of G
as discovery edges and
Trang 25Phạm Bảo Sơn - DSA 25
Trang 26DFS and Maze Traversal "
intersection, corner and
dead end (vertex) visited!
– We mark each corridor
(edge ) traversed!
– We keep track of the
path back to the entrance
(start vertex) by means
of a rope (recursion
stack)!
Trang 27Phạm Bảo Sơn - DSA 27
Properties of DFS"
Property 1!
! DFS ( G, v ) visits all the
vertices and edges in
Trang 28Analysis of DFS"
• Setting/getting a vertex/edge label takes O (1) time!
• Each vertex is labeled twice !
– once as UNEXPLORED!
– once as VISITED!
• Each edge is labeled twice!
– once as UNEXPLORED!
– once as DISCOVERY or BACK!
• Method incidentEdges is called once for each vertex!
• DFS runs in O ( n + m ) time provided the graph is
represented by the adjacency list structure!
– Recall that Σ v deg(v) = 2m
Trang 29Phạm Bảo Sơn - DSA 29
as the start vertex!
• We use a stack S to keep
track of the path between
the start vertex and the
current vertex!
• As soon as destination
vertex z is encountered,
we return the path as the
contents of the stack !
S.pop(v)
Trang 30keep track of the path
between the start vertex
and the current vertex!
• As soon as a back edge
( v, w ) is encountered,
we return the cycle as
the portion of the stack
from the top to vertex w !
Trang 32– Find and report a path with the minimum
number of edges between two given vertices !
– Find a simple cycle, if there is one!
Trang 33Phạm Bảo Sơn - DSA 33
BFS Algorithm"
• The algorithm uses a
mechanism for setting and
getting “ labels ” of vertices
for all v ∈ L i elements()
for all e ∈ G.incidentEdges(v)
Output labeling of the edges
and partition of the
Trang 35Phạm Bảo Sơn - DSA 35
Trang 37Phạm Bảo Sơn - DSA 37
!The discovery edges labeled by BFS( G,
s ) form a spanning tree T s of G s
Property 3!
!For each vertex v in L i
– The path of T s from s to v has i edges !
– Every path from s to v in G s has at least i
Trang 38• Setting/getting a vertex/edge label takes O (1) time!
• Each vertex is labeled twice !
– once as UNEXPLORED!
– once as VISITED!
• Each edge is labeled twice!
– once as UNEXPLORED!
– once as DISCOVERY or CROSS!
• Each vertex is inserted once into a sequence L i !
• Method incidentEdges is called once for each vertex!
• BFS runs in O ( n + m ) time provided the graph is
represented by the adjacency list structure!
– Recall that Σ v deg(v) = 2m
Trang 39Phạm Bảo Sơn - DSA 39
Applications"
– Compute a spanning forest of G !
– Find a simple cycle in G , or report that G is a forest! – Given two vertices of G , find a path in G between them with the minimum number of edges, or report that no such path exists!
Trang 40Spanning forest, connected
Trang 41Phạm Bảo Sơn - DSA 41
– w is in the same level as
v or in the next level in
the tree of discovery edges!
Trang 42Directed Graphs"
JFK BOS
MIA
ORD
SFO
Trang 43Phạm Bảo Sơn - DSA 43
Trang 44Digraph Properties"
– Each edge goes in one direction:!
• Edge (a,b ) goes from a to b , but not b to a.!
• If G is simple, m < n*(n-1).!
adjacency lists, we can perform listing of
in-edges and out-in-edges in time proportional to
Trang 45Phạm Bảo Sơn - DSA 45
Digraph Application"
completed before b can be started!
The good life
ics21
ics161
ics151
ics171
Trang 46vertex s determines the
vertices reachable from s
Trang 47Phạm Bảo Sơn - DSA 47
Trang 49Phạm Bảo Sơn - DSA 49
• Pick a vertex v in G.!
• Perform a DFS from v in G.!
– If there ’ s a w not visited, print “ no ” !
• Let G ’ be G with edges
reversed.!
• Perform a DFS from v in G ’ !
– If there ’ s a w not visited, print “ no ” !
– Else, print “ yes ” !
• Running time: O(n+m).!
Trang 50• Maximal subgraphs such that each vertex can reach
all other vertices in the subgraph!
• Can also be done in O(n+m) time using DFS, but is
more complicated (similar to biconnectivity).!
Trang 51Phạm Bảo Sơn - DSA 51
Transitive Closure"
• Given a digraph G , the
transitive closure of G is the
digraph G* such that!
– G* has the same vertices
Trang 52Computing the Transitive Closure"
DFS starting at
each vertex!
– O(n(n+m))!
If there's a way to get
dynamic programming: The Floyd-Warshall
Algorithm
Trang 53Phạm Bảo Sơn - DSA 53
Floyd-Warshall Transitive Closure"
Uses only vertices
numbered 1,…,k-1 Uses only vertices numbered 1,…,k (add this edge if it ’ s not already in)
Trang 54– G k has a directed edge ( v i , v j )
if G has a directed path from
• Running time: O(n 3 ),
assuming areAdjacent is O(1)
(e.g., adjacency matrix)!
Algorithm FloydWarshall(G) Input digraph G
Output transitive closure G* of G
Trang 55Phạm Bảo Sơn - DSA 55
e cann
ot be
v 3
The imag
e cann
ot
v 4
The imag
e cann
ot be
v 5
The imag
e cann
ot
v 6
v 7
Trang 56e cann
ot be
v 3
The imag
e cann
ot
v 4
The imag
e cann
ot be
v 5
The imag
e cann
ot
v 6
v 7
Trang 57Phạm Bảo Sơn - DSA 57
e cann
ot be
v 3
The imag
e cann
ot
v 4
The imag
e cann
ot be
v 5
The imag
e cann
ot
v 6
v 7
Trang 58e cann
ot be
v 3
The imag
e cann
ot
v 4
The imag
e cann
ot be
v 5
The imag
e cann
ot
v 6
v 7
Trang 59Phạm Bảo Sơn - DSA 59
e cann
ot be
v 3
The imag
e cann
ot
v 4
The imag
e cann
ot be
v 5
The imag
e cann
ot
v 6
v 7
Trang 60e cann
ot be
v 3
The imag
e cann
ot
v 4
The imag
e cann
ot be
v 5
The imag
e cann
ot
v 6
v 7
BOS
Trang 61Phạm Bảo Sơn - DSA 61
e cann
ot be
v 3
The imag
e cann
ot
v 4
The imag
e cann
ot be
v 5
The imag
e cann
ot
v 6
v 7
BOS
Trang 62e cann
ot be
v 3
The imag
e cann
ot
v 4
The imag
e cann
ot be
v 5
The imag
e cann
ot
v 6
v 7
BOS
Trang 63Phạm Bảo Sơn - DSA 63
DAGs and Topological Ordering"
• A directed acyclic graph (DAG) is a
digraph that has no directed cycles!
• A topological ordering of a digraph
is a numbering !
v 1 , …, v n !
!of the vertices such that for every
edge ( v i , v j ), we have i < j
• Example: in a task scheduling
digraph, a topological ordering a
task sequence that satisfies the
precedence constraints!
Theorem!
!A digraph admits a topological
ordering if and only if it is a DAG!
Trang 64write c.s program play
dream about graphs
A typical student day
10
11 make cookies
Trang 65Phạm Bảo Sơn - DSA 65
!
!
!
!
Algorithm for Topological Sorting"
Method TopologicalSort(G)
H ← G // Temporary copy of G
n ← G.numVertices()
while H is not empty do
Let v be a vertex with no outgoing edges Label v ← n
n ← n - 1
Remove v from H
Trang 66Topological Sorting Algorithm using DFS"
• Simulate the algorithm by using
depth-first search!
• O(n+m) time.!
Algorithm topologicalDFS(G, v)
Input graph G and a start vertex v of G
Output labeling of the vertices of G
in the connected component of v
else {e is a forward or cross edge}
Label v with topological number n
Trang 67Phạm Bảo Sơn - DSA 67
Topological Sorting Example "
Trang 68Topological Sorting Example "
9
Trang 69Phạm Bảo Sơn - DSA 69
Topological Sorting Example "
8
9
Trang 70Topological Sorting Example "
7
8
9
Trang 71Phạm Bảo Sơn - DSA 71
Topological Sorting Example "
7
8
6
9
Trang 72Topological Sorting Example "
Trang 73Phạm Bảo Sơn - DSA 73
Topological Sorting Example "
Trang 74Topological Sorting Example "
Trang 75Phạm Bảo Sơn - DSA 75
Topological Sorting Example "
Trang 76Topological Sorting Example "
Trang 78– In a flight route graph, the weight of an edge represents the
distance in miles between the endpoint airports!
MIA DFW
SFO
LAX
LGA HNL
Trang 79Phạm Bảo Sơn - DSA 79
• Given a weighted graph and two vertices u and v, we want to find
a path of minimum total weight between u and v
– Length of a path is the sum of the weights of its edges.!
SFO
LAX
LGA HNL
Trang 80Shortest Path Properties"
SFO
LAX
LGA HNL
Trang 81Phạm Bảo Sơn - DSA 81
• The distance of a vertex
v from a vertex s is the
length of a shortest path
between s and v!
• Dijkstra ’ s algorithm
computes the distances
of all the vertices from a
given start vertex s!
• Assumptions:!
– the graph is connected!
– the edges are
undirected!
– the edge weights are
nonnegative !
• We grow a “cloud” of vertices,
beginning with s and eventually
covering all the vertices!
• We store with each vertex v a label d ( v ) representing the
distance of v from s in the
subgraph consisting of the cloud and its adjacent vertices!
• At each step!
– We add to the cloud the vertex
u outside the cloud with the smallest distance label, d( u ) – We update the labels of the
vertices adjacent to u !
Trang 82Edge Relaxation"
• Consider an edge e = ( u,z )
such that!
– u is the vertex most recently
added to the cloud!
– z is not in the cloud!
• The relaxation of edge e
Trang 83Phạm Bảo Sơn - DSA 83
Trang 85Phạm Bảo Sơn - DSA 85
• A priority queue stores
the vertices outside the
for all v ∈ G.vertices()
if v = s
setDistance(v, 0) else
setDistance(v, ∞ )
l ← Q.insert(getDistance(v), v) setLocator(v,l)
while ¬ Q.isEmpty()
u ← Q.removeMin() for all e ∈ G.incidentEdges(u)
Trang 86Analysis of Dijkstra ’ s Algorithm"
• Priority queue operations!
– Each vertex is inserted once into and removed once from the priority
queue, where each insertion or removal takes O(log n ) time!
– The key of a vertex in the priority queue is modified at most deg( w )
times, where each key change takes O(log n ) time !
• Dijkstra ’ s algorithm runs in O (( n + m ) log n ) time provided the
graph is represented by the adjacency list structure!
– Recall that Σ v deg( v ) = 2 m
• The running time can also be expressed as O ( m log n ) since the graph is connected!
Trang 87Phạm Bảo Sơn - DSA 87
Shortest Paths Tree"
• Using the template
method pattern, we
can extend Dijkstra ’ s
algorithm to return a
tree of shortest paths
from the start vertex to
all other vertices!
• We store with each
vertex a third label:!
– parent edge in the
shortest path tree!
• In the edge relaxation
step, we update the
Trang 88Why Dijkstra ’ s Algorithm Works"
• Dijkstra ’ s algorithm is based on the greedy
method It adds vertices by increasing distance.!
n Suppose it didn ’ t find all shortest
distances Let F be the first wrong
vertex the algorithm processed
n When the previous node, D, on the
true shortest path was considered,
its distance was correct
n But the edge (D,F) was relaxed at
that time!
n Thus, so long as d(F)>d(D), F ’ s
distance cannot be wrong That is,
there is no wrong vertex
Trang 89Phạm Bảo Sơn - DSA 89
Negative-Weight Edges"
– If a node with a negative
incident edge were to be added
late to the cloud, it could mess
up distances for vertices already
! Dijkstra ’ s algorithm is based on the greedy
method It adds vertices by increasing distance
C ’ s true distance is 1, but it
is already in the cloud with d(C)=5!
Trang 90Bellman-Ford Algorithm"
• Works even with
negative-weight edges!
• Must assume directed
edges (for otherwise we
would have negative-weight
cycles)!
• Iteration i finds all shortest
paths that use i edges.!
• Running time: O(nm).!
• Can be extended to detect
setDistance(v, ∞ )
for i ← 1 to n-1 do for each e ∈ G.edges()
Trang 91Phạm Bảo Sơn - DSA 91
Trang 92DAG-based Algorithm"
• Works even with
negative-weight edges!
• Uses topological order!
• Doesn ’ t use any fancy
setDistance(v, ∞ )
Perform a topological sort of the vertices
for u ← 1 to n do {in topological order}
for each e ∈ G.outEdges(u)