View
216
Download
0
Tags:
Embed Size (px)
Citation preview
GraphsCSE 326 Autumn 2001
2
Graph … ADT?Graphs are a formalism useful for representing relationships between thingsA graph G is represented as G = (V, E)
V is a set of vertices: {v1, …, vn}
E is a set of edges: {e1, …, em} where each ei connects two vertices (vi1, vi2)
Operations include: iterating over vertices iterating over edges iterating over vertices adjacent to a
specific vertex asking whether an edge exists connects
two vertices
Han
Leia
Luke
V = {Han, Leia, Luke}E = {(Luke, Leia), (Han, Leia), (Leia, Han)}
GraphsCSE 326 Autumn 2001
3
Graph ApplicationsStoring things that are graphs by nature
distance between cities airline flights, travel options relationships between people, things distances between rooms in Clue
Compilers callgraph - which functions call which others dependence graphs - which variables are defined
and used at which statements
Almost anything involving discrete entities!
GraphsCSE 326 Autumn 2001
5
Partial Order: Planning a Trip
check inairport
calltaxi
taxi toairport
reserveflight
packbagstake
flight
locategate
?
GraphsCSE 326 Autumn 2001
6
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
Beware the Catch-22!
GraphsCSE 326 Autumn 2001
7
Topo-Sort Take OneLabel 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?
GraphsCSE 326 Autumn 2001
8
Topo-Sort Take TwoLabel each vertex’s in-degreePut all in-degree-zero vertices in a queue
While there are vertices remaining in the queuePick a vertex v with in-degree of zero and output itReduce the in-degree of all vertices adjacent to vPut any of these with new in-degree zero on the queueRemove v from the queue
Runtime?
GraphsCSE 326 Autumn 2001
9
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”
GraphsCSE 326 Autumn 2001
10
Adjacency MatrixA |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
Leiaruntime:space requirements:
GraphsCSE 326 Autumn 2001
11
Adjacency ListA |V|-ary list (array) in which each entry stores a list (linked list) of all adjacent vertices (or edges)
Han
Leia
LukeHan
Luke
Leia
runtime:space requirements:
GraphsCSE 326 Autumn 2001
12
In directed graphs, edges have a specific direction:(aka “di-graphs”)
In undirected graphs, they don’t (edges are two-way):
Vertices u and v are adjacent if (u, v) E
Directed vs. Undirected Graphs
LukeHan
Leia
Han
Leia
Luke
GraphsCSE 326 Autumn 2001
13
Weighted Graphs
20
30
35
60
Mukilteo
Edmonds
Seattle
Bremerton
Bainbridge
Kingston
Clinton
In a weighted graph, each edge has an associated weight or cost.
GraphsCSE 326 Autumn 2001
14
PathsA 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 = {SEA, SLC, CHI, DAL, SFO, SEA}
GraphsCSE 326 Autumn 2001
15
Path Length and CostPath length: the number of edges in the pathPath cost: the sum of the costs of each edge
Seattle
San Francisco
Dallas
Chicago
Salt Lake City
3.5
2 2
2.5
3
22.5
2.5
length(p) = 5
cost(p) = 11.5
p = {SEA, SLC, CHI, DAL, SFO, SEA}
GraphsCSE 326 Autumn 2001
16
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)
Undirected 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
Connectivity
GraphsCSE 326 Autumn 2001
18
Graph DensityA 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.
GraphsCSE 326 Autumn 2001
19
Trees as GraphsEvery 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
GraphsCSE 326 Autumn 2001
20
Directed Acyclic Graphs (DAGs)DAGs are directed
graphs with no cyclesTrees DAGs Graphs
main()
add()
access()
mult()
read()
GraphsCSE 326 Autumn 2001
21
Single Source, Shortest PathGiven a graph G = (V, E) and a vertex s V, find the shortest path from s to every vertex in V
(Strangely, this is also typically the best way to find the shortest path from s to any vertex in V)
Many variations: weighted vs. unweighted cyclic vs. acyclic positive weights only vs. negative weights allowed multiple weight types to optimize
Many applications!
GraphsCSE 326 Autumn 2001
22
The Trouble withNegative-Weight 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)
GraphsCSE 326 Autumn 2001
23
Unweighted Shortest PathAssume source vertex is C…
A
C
B
D
F H
G
E
Distance to: A B C D E F G H
GraphsCSE 326 Autumn 2001
24
Dijkstra, Edsger WybeLegendary 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.
GraphsCSE 326 Autumn 2001
25
Dijkstra’s Algorithmfor Single Source, Shortest PathClassic 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
GraphsCSE 326 Autumn 2001
27
Dijkstra PseudocodeInitialize 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: nMark n as knownFor each node a which is adjacent to n
a’s cost = min(a’s old cost, n’s cost + cost of (n, a))
GraphsCSE 326 Autumn 2001
28
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
GraphsCSE 326 Autumn 2001
29
The Known Cloud
G
Next shortest path from inside the known cloud
P
Better path to G? Not!
The Cloud Proof
But, if path to G is shortest, path to P must be at least as long. So, how can the path through P to G be shorter?
Source
GraphsCSE 326 Autumn 2001
30
Inside the Cloud (Proof)Prove by induction on # of nodes in the cloud:
Initial cloud is just the source with shortest path 0
Assume: Everything inside the cloud has the correct shortest path
Inductive step: once we prove the shortest path to G is correct, we add it to the cloud
Negative weights blow this proof away! Why?
GraphsCSE 326 Autumn 2001
31
Data Structuresfor 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:Data structure(s)?Runtime?
GraphsCSE 326 Autumn 2001
32
Dijkstra Pseudocode ReduxInitialize the cost of each node to s.cost = 0pqueue.insert(s)While (!pqueue.empty)
n = pqueue.deleteMin()For (each node a which is adjacent to n)
if (n.cost + edge[n,a].cost < a.cost) thena.cost = n.cost + edge[n,a].costa.prev = n
if (a is in the pqueue) thenpqueue.decreaseKey(a)
elsepqueue.insert(a)
GraphsCSE 326 Autumn 2001
33
Dijkstra’s Algorithm:Priority Queue OptionsRecall
Dijkstra’s uses |V| deleteMins and |E| decreaseKeys
Binary heap Straightforward implementation
Fibonacci heap Somewhat like Binomial Queues with lazy merges Amortized O(1) decreaseKey, O(log n) deleteMin Runtime with Fibonacci heaps? However, complex implementation, high-constant-overhead
Unsorted linked list??? For dense graphs, might be the way to go! Why?