29
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Embed Size (px)

Citation preview

Page 1: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

CSE 326: Data StructuresLecture #20Graphs I.5

Alon Halevy

Spring Quarter 2001

Page 2: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Outline

• Some definitions• Topological Sort• Graph Data Structures• Graph Properties• Shortest Path Problem

Page 3: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Directed vs. Undirected Graphs

Han

Leia

Luke

Han

Leia

Luke

aka: di-graphs

• In directed graphs, edges have a specific direction:

• In undirected graphs, they don’t (edges are two-way):

• Vertices u and v are adjacent if (u, v) E

Page 4: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Weighted Graphs

20

30

35

60

Mukilteo

Edmonds

Seattle

Bremerton

Bainbridge

Kingston

Clinton

There may be more information in the graph as well.

Each edge has an associated weight or cost.

Page 5: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Paths

A path is a list of vertices {v1, v2, …, vn} such that (vi, vi+1) E for all 0 i < n.

Seattle

San FranciscoDallas

Chicago

Salt Lake City

p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}

Page 6: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Path Length and Cost

Path length: the number of edges in the path

Path cost: the sum of the costs of each edge

Seattle

San FranciscoDallas

Chicago

Salt Lake City

3.5

2 2

2.5

3

22.5

2.5

length(p) = 5 cost(p) = 11.5

Page 7: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Simple Paths and CyclesA simple path repeats no vertices (except that the first can be the last):

– p = {Seattle, Salt Lake City, San Francisco, Dallas}– p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle}

A cycle is a path that starts and ends at the same node:– p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle}

A simple cycle is a cycle that repeats no vertices except that the first vertex is also the last (in undirected graphs, no edge can be repeated)

Page 8: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

ConnectivityUndirected graphs are connected if there is a path between

any two vertices

Directed graphs are strongly connected if there is a path from any one vertex to any other

Di-graphs are weakly connected if there is a path between any two vertices, ignoring direction

A complete graph has an edge between every pair of vertices

Page 9: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Graph Density

A sparse graph has O(|V|) edges

A dense graph has (|V|2) edges

Anything in between is either sparsish or densy depending on the context.

We use V and E as the parameters in our analysis.

Page 10: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Trees as Graphs

• Every tree is a graph with some restrictions:– the tree is directed

– there are no cycles (directed or undirected)

– there is a directed path from the root to every node

A

B

D E

C

F

HG

JI

Page 11: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Directed Acyclic Graphs (DAGs)

DAGs are directed graphs with no cycles.

main()

add()

access()

mult()

read()

Trees DAGs Graphs

Page 12: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Topological Sort

Given a graph, G = (V, E), output all the vertices in V such that no vertex is output before any other vertex with an edge to it.

check inairport

calltaxi

taxi toairport

reserveflight

packbags

takeflight

locategate

Page 13: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Topo-Sort Take One

Label each vertex’s in-degree (# of inbound edges)

While there are vertices remaining

Pick a vertex with in-degree of zero and output it

Reduce the in-degree of all vertices adjacent to it

Remove it from the list of vertices

runtime:

Page 14: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Topo-Sort Take Two

Label each vertex’s in-degree

Initialize a queue to contain all in-degree zero vertices

While there are vertices remaining in the queue

Pick a vertex v with in-degree of zero and output it

Reduce the in-degree of all vertices adjacent to v

Put any of these with new in-degree zero on the queue

Remove v from the queue

runtime:

Page 15: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Graph Representations

• List of vertices + list of edges

• 2-D matrix of vertices (marking edges in the cells)“adjacency matrix”

• List of vertices each with a list of adjacent vertices“adjacency list”

Han

Leia

Luke

Page 16: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Adjacency Matrix

A |V| x |V| array in which an element (u, v) is true if and only if there is an edge from u to v

Han

Leia

Luke

Han Luke Leia

Han

Luke

Leia

runtime: space requirements:

Page 17: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Adjacency List

A |V|-ary list (array) in which each entry stores a list (linked list) of all adjacent vertices

Han

Leia

LukeHan

Luke

Leia

runtime: space requirements:

Page 18: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Single Source, Shortest Path

Given a graph G = (V, E) and a vertex s V, find the shortest path from s to every vertex in V

Many variations:– weighted vs. unweighted– cyclic vs. acyclic– positive weights only vs. negative weights allowed– multiple weight types to optimize

Page 19: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

The Trouble with Negative Weighted Cycles

A B

C D

E

2 10

1-5

2

What’s the shortest path from A to E?(or to B, C, or D, for that matter)

Page 20: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Unweighted Shortest Path Problem

Assume source vertex is C…

A

C

B

D

F H

G

E

Distance to: A B C D E F G H

Page 21: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Dijkstra

Legendary figure in computer science; now a professor at University of Texas.

Supports teaching introductory computer courses without computers (pencil and paper programming)

Supposedly wouldn’t (until recently) read his e-mail; so, his staff had to print out messages and put them in his box.

Page 22: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Dijkstra’s Algorithm for Single Source Shortest Path

• Classic algorithm for solving shortest path in weighted graphs without negative weights

• A greedy algorithm (irrevocably makes decisions without considering future consequences)

• Intuition:– shortest path from source vertex to itself is 0

– cost of going to adjacent nodes is at most edge weights

– cheapest of these must be shortest path to that node

– update paths for new node and continue picking cheapest path

Page 23: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Intuition in Action

A

C

B

D

F H

G

E

2 2 3

21

1

410

8

11

94

2

7

Page 24: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Dijkstra’s Pseudocode(actually, our pseudocode for Dijkstra’s algorithm)

Initialize the cost of each node to Initialize the cost of the source to 0

While there are unknown nodes left in the graphSelect the unknown node with the lowest cost: n

Mark n as known

For each node a which is adjacent to n

a’s cost = min(a’s old cost, n’s cost + cost of (n, a))

Page 25: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Dijkstra’s Algorithm in Action

A

C

B

D

F H

G

E

2 2 3

21

1

410

8

11

94

2

7

vertex known costABCDEFGH

Page 26: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

THE KNOWNCLOUD

G

Next shortest path from inside the known cloud

P

Better pathto the same node

The Cloud Proof

But, if the path to G is the next shortest path, the path to P must be at least as long.

So, how can the path through P to G be shorter?

Source

Page 27: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Inside the Cloud (Proof)

Everything inside the cloud has the correct shortest path

Proof is by induction on the # of nodes in the cloud:– initial cloud is just the source with shortest path 0

– inductive step: once we prove the shortest path to G is correct, we add it to the cloud

Negative weights blow this proof away!

Page 28: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Revenge of the Dijkstra Pseudocode

Initialize the cost of each node to s.cost = 0;

heap.insert(s);

While (! heap.empty)

n = heap.deleteMin()

For (each node a which is adjacent to n)

if (n.cost + edge[n,a].cost < a.cost) then

a.cost = n.cost + edge[n,a].cost

a.path = n;

if (a is in the heap) then heap.decreaseKey(a)

else heap.insert(a)

Page 29: CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Data Structures for Dijkstra’s Algorithm

Select the unknown node with the lowest cost

findMin/deleteMin

a’s cost = min(a’s old cost, …)

decreaseKey

find by name

|V| times:

|E| times:

runtime:

O(log |V|)

O(log |V|)

O(1)