Graphs and Shortest Paths

Download Report

Transcript Graphs and Shortest Paths

EC-211 DATA STRUCTURES
LECTURE 15
Radix Sort
329
457
657
839
436
720
355
720
355
436
457
657
329
839
720
329
436
839
355
457
657
329
355
436
457
657
720
839
Graph Data Structure
v2
v1
v4
• A graph consists of a set of vertices
V and a set of edges E
v3
v5
v7
v8
• Undirected graph:
v6
– E consists of unordered pairs:
Edge (u, v) is the same as (v, u)
• Undirected graphs are drawn with
nodes for vertices and line
segments for edges
Directed Graph (Digraph)
v2
v1
v4
v3
v5
v7
v8
v6
• A directed graph, or digraph:
– E is set of ordered pairs, and
not necessarily a symmetric
set. Even if edge (u, v) is
present, the edge (v, u) may
be
absent
• Directed graphs are drawn with
nodes for vertices and arrows
for edges
Terminology
v2
v1
3
v4
v7
1
• A path is a sequence of vertices
v1,v2,…,vn, such that there is an
edge for each pair of consecutive
vertices
v3
• The length of a path of n vertices
-2
5
2
is n-1 (i.e. the number of edges)
may
have
weights
v5
v6 • Edges
associated with them
4
-1
• The cost of a path is the sum of
the weights of the edges along
v8
the path
Terminology
v2
v1
v4
v3
v5
v2
v1
v4
v6
v3
v5
v6
v7
v7
v8
v8
• A (simple) cycle is a path v1,v2,…,vn=v1, where the first and the
last vertices are the same
• Above, v2, v8, v6, v3, v5, v2 is a (simple) cycle in the undirected
graph, but not (even a path) in the digraph
Further terminology
• An undirected graph G is
connected if, for each pair of
vertices u, v, there is a path
that starts at u and ends at v
v1
• A digraph H that satisfies the
above condition is strongly
connected
v7
• Otherwise, if the directed graph
H is not strongly connected, but
the undirected graph G with
the same set of vertices and
edges is connected, H is said to
be weakly connected
v2
v3
v4
v5
v6
v8
v2
v1
v4
v3
v5
v7
v8
v6
Further terminology
• A complete graph contains an edge for every
pair of vertices
• On the other extreme, sparse graphs contain
very few edges
Graph Applications
•
•
•
•
•
Computer network routing
Airline scheduling
Route selection for traffic
Task scheduling
Etc.
Representing Graphs
Two ways to represent graphs:
– Adjacency matrix
• Answer “does edge (i, j) exist?” in O(1).
• Space used: O(N2) where N = number of vertices.
• Finding all neighbors of a vertex can be slow for large,
sparse graphs.
– Adjacency list
• Answer “does edge (i, j) exist?” in O(N).
• Much better space usage for large, sparse graphs.
• Finding all neighbors of a vertex is fast.
Graph Representation: Adjacency Matrix
• Let G be a graph with N vertices, where N > 0
• Let V(G) = {v1, v2, ..., vn}
• The adjacency matrix AG is a two-dimensional n × n
matrix such that the (i, j)th entry of AG is 1 if there is
an edge from vi to vj; otherwise, the (i, j)th entry is
zero
Adjacency matrix example
v2
v1
v4
v3
v5
v7
v0
v6
0
1
2
3
4
5
6
7
0
F
F
T
F
F
F
F
T
1
F
F
F
F
F
F
F
F
2
F
F
F
F
F
T
F
F
3
F
F
F
F
F
F
F
F
4
F
T
F
F
F
F
F
F
5
F
F
F
T
F
F
F
F
6
T
F
F
T
F
F
F
F
7
F
F
F
F
F
F
F
F
Graph Representation:
Adjacency Lists
• Array A of size n, such that A[i] is a pointer to the
linked list containing the vertices to which vi is
adjacent
• Each node has two components, (vertex and link)
Adjacency List Implementation of Graph
Graph Traversals — Introduction
• Depth-first search (DFS).
– Like preorder tree traversal.
– When we visit a vertex, give priority to visiting its
unvisited neighbors (and their unvisited neighbors,
etc.).
• Breadth-first search (BFS).
– Visit all of a vertex’s unvisited neighbors before
visiting their neighbors.
Graph Traversals — DFS
DFS has a nice recursive formulation:
– Given a start vertex, visit it, and mark it as visited.
– For each of the start vertex’s neighbors:
• If this neighbor is unvisited, do a DFS with this neighbor as the
start vertex.
1
2
3
5
4
6
We get a DFS tree (shown in bold above).
DFS is convenient if we think about traveling through the graph,
minimizing the number of edges we cross.
Graph Traversals — DFS
We can, as usual eliminate recursion using a Stack.
– “Last visited, first explored.”
– Then we have an iterative DFS algorithm using a local
Stack.
TRY
– Write a recursive function to do a DFS on a graph, given an
adjacency matrix.
DFS Algorithm
DFS Algorithm contd.
Graph Traversals — BFS
To do a BFS, replace the Stack with a Queue.
– “First visited, first explored.”
BFS is not as nice in several ways.
– No elegant recursive formulation.
– Not a convenient way to travel around a graph.
1
2
4
3
5
6
But BFS is useful.
– BFS is good for finding the shortest paths to other vertices.
– Also, looking for things “nearby first”.
TRY
– Rewrite the DFS function to do BFS.
BFS Algorithm
BFS Algorithm contd.
Breadth First & Depth First Spanning Trees
Breadth-first
Depth-first
Shortest Paths in Weighted Graph
v2
v1
3
v7
v3
5
v4
1
• The shortest path from a vertex u to a
vertex v in a graph is a path w1 = u,
w2,…,wn= v, where the sum:
-2
2
v5
-1
v8
v6
4
Weight(w1,w2)+…+Weight(wn-1,wn)
attains its minimal value among all paths that
start at u and end at v
• The length of a path of n vertices is n-1
(the number of edges)
• Theorem: If a graph is connected, and the
weights are all non-negative, shortest
paths exist for any pair of vertices
– Similarly for strongly connected digraphs
with non-negative weights
– Shortest paths may not be unique
Cycles and negative weights
 Negative weights may prevent the existence of
shortest paths on graphs with cycles
 In a connected graph, shortest paths exist if and
only if no negative cost cycles exist
How to compute shortest paths
• case 1: unweighted edges
2
v2
v1
v4 4
v7
v3
1
v5
1
v6
3
v8 2
Shortest path from v3 to v4
• conceptually equivalent to
have all edges of same weight
• shortest path is the minimum
number of edges;
• Approach: Breadth-First
Search
Shortest Path – Dijkstra’s Algorithm
vertexSet = {}, Parent[ ] = none for all nodes
C[start] = 0, C[ ] =  for all other nodes
while ( not all nodes in vertexSet )
find node v not in vertexSet with smallest C[v]
add v to vertexSet
for each node J not in vertexSet adjacent to v
if ( C[v] + cost of (v,J) < C[J] )
C[J] = C[v] + cost of (v,J)
Parent[J] = v
Optimal solution computed with greedy algorithm
Dijkstra’s Algorithm: Example
Dijkstra’s Algorithm: Example
contd.