CS211, Lecture 21 Graphs shortest path algorithm - PowerPoint PPT Presentation

About This Presentation
Title:

CS211, Lecture 21 Graphs shortest path algorithm

Description:

Is it possible to travel over the bridges so that each bridge is walked over exactly once? ... an enumerator over edges with start vertex w. next() returns an Edge ... – PowerPoint PPT presentation

Number of Views:119
Avg rating:3.0/5.0
Slides: 24
Provided by: ping50
Category:

less

Transcript and Presenter's Notes

Title: CS211, Lecture 21 Graphs shortest path algorithm


1
CS211, Lecture 21 Graphsshortest path algorithm
Readings Weiss, secs. 14.1--14.3, sec. 14.5.1.
"This 'telephone' has too many shortcomings to be
seriously considered as a means of
communications. " - Western Union internal memo
- 1876 "I think there is a world market for
maybe five computers." - Watson, chair of IBM -
1943 "The problem with television is that the
people must sit and keep their eyes glued on a
screen the average American family hasn't time
for it. - The New York Times - 1949 "Where
... the ENIAC is equipped with 18,000 vacuum
tubes and weighs 30 tons, computers in the future
may have only 1,000 vacuum tubes and weigh only
1.5 tons. - Popular Mechanics - 1949 "There is
no reason anyone would want a computer in their
home." - Ken Olson, founder DEC - 1977 "640K
ought to be enough for anybody. - Bill Gates -
1981 "By the turn of this century, we will live
in a paperless society." - Roger Smith, chair GM
- 1986 "I predict the Internet... will go
spectacularly supernova and in 1996
catastrophically collapse." - Bob Metcalfe,
inventor and 3Com founder - 1995
did he mean memory or money?
2
Graphs
A graph is a set V of nodes (or vertices)
together with a set E of edges between them.
Here, graph could represent roads between cities,
or airplane flights between cities.
Nodes V SF, NY, LA, chic, london, paris
Edges (SF, LA), (SF, NY), (SF, chic), (LA,
chic) (LA, chic), (NY, chic),
(london, paris)
V size of V number of nodes (here, 6) E
size of E number of edges (here, 7)
3
Leonhard Euler (1707--1783) started graph theory
Koenigsberg, Prussia, 1736.
land
island
island
land
allows for more than one edge between two vertices
Koenigsberg bridges. Is it possible to travel
over the bridges so that each bridge is walked
over exactly once? Extend to graph theory For
which graphs is it possible to find a path that
contains each edge exactly once?
4
Important graph problems
Traveling salesman problem (TSP) Graph is a
bunch of cities, edges have weights that give the
cost for traveling from one city to another. Find
the cheapest way to travel to all cities and end
up back at the home city. Four-color problem. Can
one color countries of a map using four colors so
that no two adjacent countries have the same
color? Nodes countries. Edges edge from one
country to another if they have a common
boundary. Graph is planar --can draw so that no
two edges intersect. Color nodes of a graph using
four color so that no two adjacent nodes have the
same color? About 1870. An incorrect proof
published error detected 10 years later. 1977
Appel and Haken used computers to solve it (yes,
one can).
5
Directed graph
A directed graph, or digraph, is a set V of
vertices together with a set E of directed edges
(arrows) between them. Only one edge allowed in a
particular direction between the nodes. We
concentrate on directed graphs. Below,
stands forhttp//www.cs.cornell.edu/courses/cs211
/2004sp

http//java.sun.com/j2se/1.4.2/download.html
/index.html
/bootcamp.html
DrJava.html
6
Celebrity problem uses a directed graph
At a party, a celebrity is a person who everyone
knows and who knows no one (except
themselves). How much time does it take to find a
celebrity (if present)? Graph nodes are people,
Edge (p1, p2) if p1 knows p2. Its a directed
graph. Originally thought this required required
time O(VV). Can you write an O(V)
algorithm to find the celebrity?
7
Directed graph
Sometimes, we put positive weights on the edges.
If we dont assume the weight is one. For a map
of roads between cities, the weight might be the
shortest mileage.


http//java.sun.com/j2se/1.4.2/download.html
4
/index.html
3
8
9
6
/bootcamp.html
DrJava.html
8
Directed graph
A path is a sequence of edges that connect by
successive vertices. The length of the path is
the number of edges. Simple path all vertices
(except possibly first and last)
are different. Cycle simple path in which
first and last vertices are the same. A graph
without cycles is called an acyclic graph.
path (V1, v2, v0) has length 2 path (V1, V2) has
length 1 path (V1) has length 0 path (V1, V2, V0,
V1, V2) has length 4
9
Adjacency matrix representation of a digraph
Use a boolean array b0..V-10..V1 brc
there is an edge from Vr to Vc
0 1 2 3 0 F T F F 1 F F T F 2
T F F F 3 T T T F
Constant time to tell whether there is an edge
from r to c. Good. Takes VV time to
construct, VV space, and VV time to
process all edges. No good if graph is sparse
(very few edges in relation to number of nodes).
Usually, sparse means that the number of edges is
O(V).
10
Adjacency list representation of a digraph for
sparse graphs
Use array b0..V1 of linked lists If (h, k)
is an edge, k is on linked list bh.
No prescribed order of values in a linked
list. Time to construct O(V) O(E) Time to
tell whether an edge is there O(V) (worst
case) Time to process the edges O(E)
11
Dijkstras shortest path algorithm (assuming
edges have positive weights)
Given a path from w to v, we can count the
weights that are on the edges of that path. Call
the sum the cost of the path. Find the shortest
path the path with minimum cost from a start
vertex v to each other vertex.
1
5
3
Shortest path from V3 to V1 is (V3, V2, V0, V1)
9
1
9
12
Find shortest paths from v to all nodes
Let n V i.e. the number of vertices Store
values in L0..n-1 so that R Lk the
shortest path length from w to k, for 0 k lt n
(if there is no path from v to k, set Lk
to 8 or, can use Integer.MAX_VALUE
instead of 8) For the graph on this slide, with
w V3, L 2, 7, 1, 0
13
Find shortest paths from v to all nodes
Initially, Lv 0 // shortest path from
v to v Lw 8 // for all
other nodes red set
  • red set the set of vertices
  • whose L-value has been calculated
  • whose neighbors have L-values lt 8
  • Frontier vertices that
  • are not in the red set
  • have L-value lt 8

3
4
v
1
14
Find shortest paths from v to all nodes
Initially, Lv 0 // shortest path from
v to v We put the L-values in the nodes
themselves.
  • red set the set of vertices
  • whose L-value has been calculated
  • whose neighbors have L-values lt 8
  • Frontier vertices that
  • are not in the red set
  • have L-value lt 8

4
For a node w in Frontier, Lw is the minimum
path length over (v,w) paths using only red nodes
(except for w).
15
Find shortest paths from v to all nodes
Initially, Lv 0 // shortest path from
v to v We put the Lvalues in the nodes
themselves.
  • red set the set of vertices
  • whose L-value has been calculated
  • whose neighbors have L-values lt 8
  • Frontier vertices that
  • are not in the red set
  • have L-value lt 8

For a node w in Frontier, Lw is the minimum
path length over (v,w) paths using only red nodes
(except for w).
16
At each iteration Let f be the Frontier node
with smallest L value. Make f red and for each
node w adjacent to f if (Lf weight (f,w)
lt Lw) Lw Lf weight(f,w) Put w
in Frontier (make blue)
Invariant shows only edges from red to blue,
blue to black
17
Lw 8 for all nodes w Lv 0 F v while
(F not empty) f a node in F with min L
value Make f red (delete from F) for each
node w adjacent to f if (Lf weight
(f,w) lt Lw) Lw Lf weight(f,w)
if (w is not in F) put w in F

Invariant shows only edges from red to blue,
blue to black
18
Lw 8 for all nodes w Lv 0 F v while
(F not empty) // outer
loop V iterations f a node in F with min
L value // done V times Make f
red (delete from F) // done
V times for each node w adjacent to f
if (Lw 8)
// done E times Lw Lf
weight(f,w) // done lt V times
Put w in F
// done lt V times else if (Lf
weight (f,w) lt Lw) // done lt E V
times Lw Lf weight(f,w)
// done lt E V times
How much time?
What data structure do we use for F?
19
Lw 8 for all nodes w Lv 0 F v while
(F not empty) f a
node in F with min L value Make f
red (delete from F) for
each node w adjacent to f if (Lw 8)

Lw Lf weight(f,w)
Put w in F
else if (Lf weight (f,w) lt
Lw) Lw Lf weight(f,w)

What data structure do we use for F? A min-heap
--a heap with minimum at the top.
f node with min L value constant time Delete
f from F log F time Put w in F log F
time Total time O(V log V)
20
/ An instance is an edge / public class Edge
public int start // An edge from vertex
start public int end // to vertex end
public int weight // the weight of the edge (gt
0) / Constructor and edge (start, end)
with weight w / public Edge(int start, int
end, int w) this.start start
this.end end weight w
21
public class AdjacencyList private LNode
V // The adjacency list for the graph
private int Esize // number of edges
/ Constructor a graph of n nodes, with e
edges given by edge set E0..e-1.
Precondition the edges are all different. /
public AdjacencyList(int n, int e, Edge E)
/ number of nodes in graph /
public int Vsize() / number of
edges in graph / public int Esize()
/ an enumerator over edges with start
vertex w next() returns an Edge /
public Iterator adjacencyEnumerator(int w)

22
/ an array of shortest paths from node v to
other nodes in graph g / public static int
shortest(AdjacencyList g, int v) int
L new intg.Vsize() // The shortest-path
lengths from v for (int k 0 k !
L.length k k1) Lk
Integer.MAX_VALUE Lv 0
int F new intg.Vsize() // Set F is in
min-heap int Fsize 0 //
F0..Fsize-1 // Add v to heap F
and store the new heap size in Fsize
Fsize addHeap(F, Fsize, v) //
Invariant The red nodes have their L-values
calculated and their
neighbors have L-values lt MAX_VALUE.
F is the set of nodes with L-value
lt MAX_VALUE that are
not red.
23
while (Fsize ! 0) int f
F0 Fsize removeMinHeap(F, Fsize)
Iterator it g.adjacencyEnumerator(f)
while (it.hasNext()) Edge
e (Edge)it.next()
if (Le.end Integer.MAX_VALUE)
Le.end Lf e.weight
Fsize addHeap(F, Fsize, e.end)
else // e.end is
already in F if (Lf
e.weight lt Le.end)
Le.end Lf e.weight
BubbleUp(F, Fsize, e.end)

return L
Write a Comment
User Comments (0)
About PowerShow.com