Chapter-15-Graphs

Chapter 15
Graphs
© 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Overview
●
15.1
–
●
15.2
–
●
Discussion of graph terminology.
Representation
15.3
–
Traversal
Overview
●
15.5
–
●
Algorithms for finding shortest paths
15.6
–
Minimum spanning tree
–
Graphs generalize trees.
–
Graphs may have more than one path between two
nodes.
Terminology
●
Graph
–
Consists of vertices (nodes) and edges.
●
●
Vertices are circles.
Edges are lines or arrows.
Terminology
●
●
Directed graph
–
Edge is an arrow going from one vertex to another.
–
In some situations, it is meaningful to have a selfloop.
Undirected graph
–
Edge is simply an line connecting two vertices.
●
●
Undirected graphs usually do not have self-loops.
For every undirected graph there is a corresponding
directed graph where each undirected edge is replaced
by two directed edges, one in each direction.
Terminology
Terminology
●
Neighbors
–
●
Path
–
●
Vertices that can be reached by moving along
one edge.
A sequence of vertices, each a neighbor of the
previous one.
Length
–
Length of the path is the number of edges along the
path.
Terminology
●
Distance
–
●
Cycle
–
●
Distance between two vertices is the length of
the shortest path between them.
A path (of length one or more) from a vertex back to
itself.
Weighted graphs
–
Data associated with the edges.
Terminology
Terminology
Terminology
Terminology
Terminology
●
A graph with no cycles is called acyclic.
–
dag
●
Directed acyclic graph
Terminology
●
A graph in which every pair of vertices is
connected by a path is said to be connected.
Terminology
●
Weighted graph.
–
We can associate data with an edge.
Terminology
●
When analyzing graph algorithms, we often give
results in terms of v (the number of vertices)
and e (the number of edges).
●
In a directed graph, e ≤ v2.
●
In an undirected graph:
Terminology
●
In either case e  O(v2)
●
Dense
–
●
A graph with close to the maximum number of
edges.
Sparse
–
A graph with far fewer edges.
Representation
●
Neighbor list or adjacency list
–
The amount of memory used is in Θ(v + e).
–
The time to check for a particular edge is in O(e).
Representation
●
If a graph is dense, it is better to use a neighbor
matrix or adjacency matrix.
–
This is essentially a two-dimensional array of
booleans.
–
<i, j> is true when there is an edge from vertex i to
vertex j.
–
Memory used is Θ(n2).
–
The time to check for a particular edge is constant.
–
Easily adapted to handle weighted graphs.
–
0 if there is no edge.
Representation
Representation
Representation
Representation
Representation
Representation
Representation
Representation
Representation
Representation
Representation
Graph Traversal
●
Source vertex
–
●
Depth-first traversal
–
●
We must choose an arbitrary vertex to start from.
Follows edges until it reaches a dead end, then
backtracks to the last branching point.
Breadth-first
–
Visits one vertex, then its neighbors, then vertices
two edges away, and so on.
Graph Traversal
●
Vertices that cannot be reached from the
source are never visited.
–
It is necessary to keep track of which vertices have
already been visited.
–
This is done with an array of booleans.
Graph Traversal
●
Depth-first
Graph Traversal
●
Breadth-first
Graph Traversal
Graph Traversal
Graph Traversal
Shortest Paths
●
A common graph problem is finding the shortest
path between two vertices.
–
We want to minimize the sum of the weghts of the
edges on the path, not the number of vertices.
–
How much does it cost to get directly from one
vertex to another.
●
●
●
Weight of the edge is the cost.
If they are not connected, the cost is infinite.
Double.POSTITIVE_INFINITY
–
Double.POSTITIVE_INFINITY + 1 ==
Double.POSTIVITE_INFINITY
Shortest Paths
Shortest Paths
●
Two algorithms
–
Dijkstra's single source algorithm determines the
distances from one vertex to all others.
–
Floyd-Warshall all pairs algorithm, which might be
used to create a table of distances in a road atlas,
determines the distance from each vertex to each
other vertex.
Shortest Paths
●
Dijkstra's Single-Source Algorithm
–
Maintains an array containing the distance to each
vertex.
–
Initially, the distance to the source vertex is 0 and
all other distances are infinite.
Shortest Paths
–
The vertices are visited in order of increasing
distance from the source.
–
By the time we visit a vertex, we have visited all of
the vertices along the shortest path to that vertex.
–
Similar to a breadth-first traversal, but the weights
are taken into account.
–
result[i] + getCost(i, j)
●
●
If this sum is smaller, we have found a shorter path to j,
so we update result[j]
Total running time Θ(v2)
Shortest Paths
Shortest Paths
Shortest Paths
●
Floyd-Warshall All-Pairs Algorithm
–
To find the shorest path between each pair of
vertices, we could just run Dijkstra's algorithm once
from each vertex, using time in Θ(v3)
–
Floyd-Warshall all-pairs takes time in this order, but
it is somewhat simpler.
●
●
Uses dynamic programming.
result[i] [j] is the shortest known distance from vertex i to
vertex j.
Shortest Paths
●
Questions to ask:
–
What is the shortest distance between each pair of
vertices using vertex 0 as an intermediate point?
–
What is the shortest distance between each pair of
vertices using vertices 0 and 1 as intermediate
points?
–
What is the shortest distance between each pair of
vertices using vertices 0 through 2 as intermediate
points?
Shortest Paths
●
When possible intermediate points have been
considered the distances are correct.
–
Two possibilities for each pair of vertices i and j:
●
●
●
The shortest path using vertices 0 through 5 as
intermediate points does not involve vertex 5 (It was
already correct).
The shortest path using vertices 0 through 5 as
intermediate points does involve vertex 5. It must be
result[i][5] + result[5][j]. Neither of these two subpaths
can have vertex 5 as an intermediate point, because it is
an endpoint.
Running time in Θ(v3)
Shortest Paths
Shortest Paths
Minimum Spanning Trees
●
●
Connect a set of vertices while minimizing
expenses.
Spanning tree
–
Given a connected, undirected graph G, a spanning
tree is another graph which has all of G's vertices
and a subset of G's edges
–
Spanning tree has only v – 1 edges.
●
●
●
Minimum number of edges required to connect all of the
vertices.
Never contains a cycle.
May be more than one spanning tree for a given graph.
Minimum Spanning Trees
●
In a weighted graph, a minimum spanning tree
is a spanning tree in which the sum of the edge
weight is as small as possible.
–
There may be more than one minimum spanning
tree for a given graph.
–
Kruskal's minimum spanning tree algorithm.
●
●
●
Begin with an edgeless graph, then add edges until we
have a minimum spanning tree.
Add the cheapest edge that we haven't tried before.
If there was not already a path between the vertices at
the ends of the edge, we add the edge.
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Trees
●
This is a Greedy algorithm
–
Always does whatever seems best in the short
term.
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Trees
●
Total running time:
Summary
●
A graph is a collection of vertices connected by
edges.
–
Directed or undirected, weighted or unweighted,
and connected or unconnected.
–
A graph with no cycles is called acyclic.
–
Graphs are normally represented using neighbor
lists or neighbor matrices.
–
Neighbor lists use less space for sparse graphs.
–
Neighbor matrices take less time in either case.
Summary
●
●
●
●
A graph may be traversed depth-first.
A topological sort of a directed acyclic graph is
an ordering of the vertices such that no edges
point from a later vertex to an earlier vertex.
Finding shortest paths in a weighted graph
eliminates some edges so as to stay connected
while minimizing the total edge weight.
Kruskal's minimum spanning tree algorithm is a
greedy algorithm.