vertices can hold arbitrary objects: the labels, making
this a labeled graph
int [] neighbors (int vertex) method computes the neighbors
of a vertex
number of vertices is fixed; edges may change
public class Graph {
private boolean [] [] edges;
private Object [] labels;
public Graph (int n) {
edges = new boolean [n] [n];
labels = new Object [n]; // null
}
Graph Traversal
tree traversals: breadth-first or depth-first (depth-first is
usually implemented by recursion so can be pre-order, in-order, or post-order)
graph traversals: breadth-first, or depth-first
we follow directed links to find all the vertices in the graph:
some vertices may be unreachable from our root vertex
to detect loops, we mark each node we see, and avoid re-visiting
marked nodes (like maze exploration)
Depth-first traversal
recursive traversal
mark a vertex, then traverse all of the vertices reachable
from this node
stopping conditions:
a vertex has no outgoing edges, or
all outgoing edges lead to marked vertices
marking prevents cycles from causing infinite recursion
Breadth-first traversal
use a queue
mark the root element
insert the root into the queue
repeat until the queue is empty:
remove the first element in the queue
mark all unmarked vertices reachable from this vertex, and
add them to the queue
Graphs with weighted Edges
a succession of vertices such that each has an edge to the next
is a path
a weighted edge has a weight or cost that is an
integer greater than or equal to 1
examples:
distances between highway exits
time between airports or train stations
expected delay on network links
cost of shipping to different destinations
unweighted edges have cost 1
in general, we want to find a path from vertex A to vertex B
that minimize the cost: the shortest path from A to B
In-class exercise
work alone
5 minutes
find the shortest path from A to B
if you have time, describe the algorithm you used
Dijkstra's Shortest Path Algorithm
compute the distance from a given vertex to all other reachable
vertices in the graph
example graph:
Dijkstra's Shortest Path Algorithm
distances array:
one element for each vertex
integer stores distance from start
all integers initialized to oo, except start
distance initialized to zero
set of allowed vertices A:
initially empty
distances has correct distance for all vertices in this set
all but the last vertex on a permitted path must be in this set
Dijkstra's Shortest Path Algorithm Main Loop
search the distances array to find the closest vertex NOT
yet in the allowed set (any one if there is several with the same
distance). Call this vertex next
add next to the allowed set A
revise the distances array for all the neighbors of next,
since this new vertex may now appear on permitted paths
Why does this work?
Invariants:
we know the distance for all vertices in A
the distance for all vertices in A is less than for
any vertices not in A
we know the distance d for all neighbors of the vertices in A,
including the nearest vertex NOT in A
when we add next to A:
next is farther than anything in A, so cannot
decrease their distance
for any neighbor of next connected with an edge of
cost dv, if d+dv is less than its old distance, we update that distance
Keeping Track of the Path
keep a parallel array predecessor as well as distance
whenever you update distance, record in
predecessor the vertex through which this distance is calculated
at the end, can reconstruct the path to any vertex v:
int p = predecessor[v] is the node before v on the path
predecessor[p] is the node two before v on the path
can use a stack to print the path in the forward order
Find the closest vertex not in A
search through the distances array
looking for the one with the smallest distance
that is not in A
Add this vertex to A
can keep a set using any of the strategies studied in this course
Update the distance of the neighbors of next
for each vertex v in set of neighbors of next,
and given that next is at distance d,
and that the edge from next to v has cost dv
if (distances [v] > d + dv) then
distances [v] = d + dv; and
predecessor [v] = next;