1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1....

23
1 CS211, Lecture 21 Graphs shortest 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 did he mean memory or money?

Transcript of 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1....

Page 1: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

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 consideredas 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?

Page 2: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

2

A graph is a set V of nodes (or vertices) together witha set E of edges between them. Here, graph could represent roads between cities, or airplane flights between cities.

Graphs

NY

chic

LA

london

parisSF

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)

Page 3: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

3

Leonhard Euler (1707--1783) started graph theory: Koenigsberg, Prussia, 1736.

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?

island island

land

land

land 1

land 2

island 1island 2

allows for more than one edge between two vertices

Page 4: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

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).

Page 5: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

5

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 for“http://www.cs.cornell.edu/courses/cs211/2004sp”

Directed graph

…/bootcamp.html

…/index.html

…DrJava.html

http://java.sun.com/j2se/1.4.2/download.html

Page 6: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

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.

It’s a directed graph.

Originally: thought this required required time O(|V|*|V|).

Can you write an O(|V|) algorithm to find the celebrity?

Page 7: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

7

Sometimes, we put positive weights on the edges. If we don’t assume the weight is one.

For a map of roads between cities, the weight might be the shortest mileage.

Directed graph

…/bootcamp.html

…/index.html

…DrJava.html

http://java.sun.com/j2se/1.4.2/download.html

4

3

69

8

Page 8: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

8

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.

Directed graph

V1

V0

V3

V2

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

Page 9: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

9

Use a boolean array b[0..|V|-1][0..|V|–1]

b[r][c] = “there is an edge from Vr to Vc”

Adjacency matrix: representation of a digraph

V1

V0

V3

V2

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 |V|*|V| time to construct, |V|*|V| space, and |V|*|V| 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|).

Page 10: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

10

Use array b[0..|V|–1] of linked lists

If (h, k) is an edge, k is on linked list b[h].

Adjacency list: representation of a digraph for sparse graphs

V1

V0

V3

V2

0

1

2

3

1

2

0

1 0 2

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|)

Page 11: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

11

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.

Dijkstra’s shortest path algorithm (assuming edges have positive weights)

V1

V0

V3

V2

9

35

9

1

1

Shortest path from V3 to V1 is: (V3, V2, V0, V1)

Page 12: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

12

Let n = |V| –i.e. the number of vertices

Store values in L[0..n-1] so that:

R: L[k] the shortest path length from w to k, for 0 ≤ k < n

(if there is no path from v to k, set L[k] to ∞

or, can use Integer.MAX_VALUE instead of ∞)

For the graph on this slide, with w = V3,

L = { 2, 7, 1, 0 }

Find shortest paths from v to all nodes

V1

V0

V3

V2

9

35

9

1

1

Page 13: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

13

Initially, L[v] = 0 // shortest path from v to v

L[w] = ∞ // for all other nodes

red set = {}

Find shortest paths from v to all nodes

v

3

4

1

red set: the set of vertices(1) whose L-value has

been calculated(2) whose neighbors have

L-values < ∞Frontier: vertices that (1) are not in the red set(2) have L-value < ∞

Page 14: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

14

Initially, L[v] = 0 // shortest path from v to v

We put the L-values in the nodes themselves.

Find shortest paths from v to all nodes

4

v

3

1

0

3

4

1

26

3

red set: the set of vertices(1) whose L-value has

been calculated(2) whose neighbors have

L-values < ∞Frontier: vertices that (1) are not in the red set(2) have L-value < ∞

For a node w in Frontier, L[w] is the minimum path length over (v,w) paths using only red nodes (except for w).

Page 15: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

15

Initially, L[v] = 0 // shortest path from v to v

We put the L=values in the nodes themselves.

Find shortest paths from v to all nodes

v

3

4

1

0

3

3

1

26

3

7

3

red set: the set of vertices(1) whose L-value has

been calculated(2) whose neighbors have

L-values < ∞Frontier: vertices that (1) are not in the red set(2) have L-value < ∞

For a node w in Frontier, L[w] is the minimum path length over (v,w) paths using only red nodes (except for w).

Page 16: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

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 (L[f] + weight (f,w) < L[w]) {

L[w]= L[f] + weight(f,w);

Put w in Frontier (make blue);

}

Invariant: shows only edges

from red to blue, blue to black

Frontier F. For w in here,L[w] is min path lengthover paths from v to w forwhich all nodes are redexcept w.

Red set. For red w, L[w]has been calculated.Neighbors have L[w] < ∞

rest of nodes5

7

3

1

2

4

5

Page 17: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

17

L[w]= ∞ for all nodes w;

L[v]= 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 (L[f] + weight (f,w) < L[w]) {

L[w]= L[f] + weight(f,w);

if (w is not in F) put w in F;

}

}

Frontier F. For w in here,L[w] is min path lengthover paths from v to w forwhich all nodes are redexcept w.

Red set. For red w, L[w]has been calculated.Neighbors have L[w] < ∞

rest of nodes

Invariant: shows only edges from red to blue, blue to black

5

7

3

1

2

4

5

Page 18: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

18

L[w]= ∞ for all nodes w;

L[v]= 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 (L[w] = ∞) { // done |E| times

L[w]= L[f] + weight(f,w); // done < |V| times

Put w in F; // done < |V| times

} else if (L[f] + weight (f,w) < L[w]) // done < |E| – |V| times

L[w]= L[f] + weight(f,w); // done < |E| – |V| times

}5

7

3

1

2

4

5

How much time?

What data structure do we use for F?

Page 19: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

19

L[w]= ∞ for all nodes w;

L[v]= 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 (L[w] = ∞) {

L[w]= L[f] + weight(f,w);

Put w in F;

} else if (L[f] + weight (f,w) < L[w])

L[w]= L[f] + weight(f,w);

}5

7

3

1

2

4

5

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|)

Page 20: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

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 (> 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; }}

Page 21: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

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 E[0..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) {} }

Page 22: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

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 int[g.Vsize()]; // The shortest-path lengths from v for (int k= 0; k != L.length; k= k+1) L[k]= Integer.MAX_VALUE; L[v]= 0; int[] F= new int[g.Vsize()]; // Set F is in min-heap int Fsize= 0; // F[0..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 < MAX_VALUE. F is the set of nodes with L-value < MAX_VALUE that are not red. …

Page 23: 1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

23

while (Fsize != 0) { int f= F[0]; Fsize= removeMinHeap(F, Fsize); Iterator it= g.adjacencyEnumerator(f); while (it.hasNext()) { Edge e= (Edge)it.next(); if (L[e.end] == Integer.MAX_VALUE) { L[e.end]= L[f] + e.weight; Fsize= addHeap(F, Fsize, e.end); } else { // e.end is already in F if (L[f] + e.weight < L[e.end]) { L[e.end]= L[f] + e.weight; BubbleUp(F, Fsize, e.end); } } } } return L;}