Lecture 18
Lecture 18
Lecture #18
Exploring Graphs
Bart Niswonger
Summer Quarter 2001
Today’s Outline
• Stuff Bart didn’t finish Friday
• Graph Algorithms
– Shortest Path
• Djikstra
– Minimum Spanning Tree
• Kruskal
• Prim
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
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
Dijkstra’s Pseudocode
(actually, our pseudocode for Dijkstra’s algorithm)
findMin/deleteMin
|E| times:
a’s cost = min(a’s old cost, …)
decreaseKey
find by name
runtime:
Revenge of 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 (heap.contains(a)) then
heap.decreaseKey(a)
else heap.insert(a)
Single Source & Goal
Suppose we only care about
shortest path from source s to a
particular vertex g
– Run Dijkstra to completion
– Stop early? When?
• When g is added to the priority queue
• When g is removed from the priority
queue
• When the priority queue is empty
Spanning Trees
Spanning tree: a subset of the edges from a
connected graph that…
…touches all vertices in the graph (spans the
graph)
…forms a tree (is connected and contains no
cycles)
4 7
9
2
1 5
• VLSI design
• Transportation systems
2 2 3
A B F H
2
1 1
4 9 10
G
C 4
2 8
D
E
7
Kruskal’s Algorithm in Action (2/5)
2 B 2 3
A F H
2
1 1
4 9 10
G
C 4
2 8
D
E
7
Kruskal’s Algorithm in Action (3/5)
2 B 2 3
A F H
2
1 1
4 9 10
G
C 4
2 8
D
E
7
Kruskal’s Algorithm in Action (4/5)
2 B 2 3
A F H
2
1 1
4 9 10
G
C 4
2 8
D
E
7
Kruskal’s Algorithm Completed (5/5)
2 B 2 3
A F H
2
1 1
4 9 10
G
C 4
2 8
D
E
7
Why Greediness Works
The algorithm produces a spanning tree. Why?
Proof by contradiction: Kruskal’s finds the minimum:
Assume another spanning tree has lower cost than
Kruskal’s
Pick an edge e1 = (u,v) in that tree that’s not in Kruskal’s
Kruskal’s connects u’s and v’s sets with another edge e2
But e2 must have at most the same cost as e1!
So, swap e2 for e1 (at worst keeping the cost the same)
Repeat until the tree is identical to Kruskal’s:
contradiction!
|E| times:
Pick the lowest cost edge… findMin/deleteMin
|E| times:
If u and v are not already connected… union
…connect u and v.
stack(blue,red)
stack(green,red)
Blocks World
source: initial state of the blocks
goal: desired state of the blocks
path from source to goal = sequence
of actions (program) for robot arm!
• n blocks nn states
• 10 blocks 10 billion states
Problem: Branching Factor
• Dijkstra’s algorithm is basically breadth-
first search (modulo arc weights)
– Visits all nodes (exhaustive search)
• Suppose we know that goal is only d steps
away.
• If out-degree of each node is 10,
potentially visits 10d vertices
– 10 step plan => 10 billion vertices!
S
52 St
nd
G
51st St
50th St
10th Ave
9th Ave
8th Ave
2nd Ave
3rd Ave
7th Ave
6th Ave
5th Ave
4th Ave
Best-First Search
The Manhattan distance ( x+ y) is an
estimate of the distance to the goal
– a heuristic value
Best-First Search
– Order nodes in priority to minimize
estimated distance to the goal
Compare: Dijkstra
– Order nodes in priority to minimize
distance from the start
Best First in Action
• Suppose you live in Manhattan; what do you do?
S
52 St
nd
G
51st St
50th St
10th Ave
9th Ave
8th Ave
2nd Ave
3rd Ave
7th Ave
6th Ave
5th Ave
4th Ave
2nd Ave
G
3rd Ave
Being Mislead
4th Ave
5th Ave
• Will get back on track!
6th Ave
7th Ave
8th Ave
9th Ave
S
10th Ave
50th St
52 St
51st St
nd
Optimality
• Does Best-First Search find the
shortest path
– when the goal is first seen?
– when the goal is removed from
priority queue?
Sub-Optimal Solution
• Goal is by definition at distance 0: will be
removed from priority queue immediately,
even if a shorter path exists!
52nd St
S (5 blocks)
51st St
G
9th Ave
8th Ave
7th Ave
6th Ave
5th Ave
4th Ave
Synergy?
Dijkstra / Breadth First guaranteed to find
optimal solution
Best First often visits far fewer vertices,
but may not provide optimal solution
52 St
nd
S (5 blocks) 5+2=6
51st St
G
1+4=5
50th St
Dijkstra
9th Ave
would
8th Ave
7th Ave
6th Ave
5th Ave
4th Ave
have
visited
these
guys!
Revised Cloud Proof
• Suppose have found a path of cost c to G which is not optimal
– priority(G) = f(G) = g(G) + h(G) = c + 0 = c
• Say N is the last vertex on an optimal path P to G which has been
added to the queue but not yet dequeued.
– There must be such an N, otherwise the optimal path would have been found.
– priority(N) = f(N) = g(N) + h(N) g(N) + actual cost N to G
= cost of path P < c
• So N will be dequeued before G is dequeued
• Repeat argument to show entire optimal path will be expanded before G
is dequeued.
N
S G
c
A Little History
• A* invented by Nils Nilsson &
colleagues in 1968
– or maybe some guy in Operations
Research?
• Cornerstone of artificial intelligence
– still a hot research topic!
– iterative deepening A*, automatically
generating heuristic functions, …
• Method of choice for search large
(even infinite) graphs when a good
heuristic function can be found
What About Those Blocks?
• “Distance to goal” is not always physical
distance
• Blocks world:
– distance = number of stacks to perform
– heuristic lower bound = number of blocks out of place
essentially, nothing.
To Do
• Project IV
– Write a Graph Data Structure!
• Start reading Chapter 10
• Think about your favorite T-shirt
Coming Up
• Kinds of algorithms
• No Quiz!
• Other Data Structures