You are on page 1of 33

Chapter 14

Graphs and Paths


Graph
• A geometric diagram consisting of a finite
number of dots called vertices joined by a
finite number of curved or straight line
segments called edges.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-2


History: Seven Bridges of Königsberg

• The city of Königsberg, Prussia (now


Kaliningrad, Russia) is set on the Pregel
River, and included two large islands which
were connected to each other and the
mainland by seven bridges.
• The problem is to decide whether it is
possible to walk a route that crosses each
bridge exactly once.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-3
Seven Bridges of Königsberg

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-4


Graph Theory
• In 1736, Leonhard Euler proved that it was
not possible.
• In proving the result, Euler formulated the
problem in terms of graph theory,
eliminating all features except the
landmasses and the bridges connecting them
and replacing each landmass with a dot,
called a vertex or node, and each bridge with
a line, called an edge or link.
• The resulting mathematical structure is
called a graph.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-5
Graph Theory (2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-6


Definitions
• A graph consists of a set of vertices and set
of edges that connect the vertices.
• That is, G = (V, E), where V is the set of
vertices and E is the set of edges.
• Each edge is a pair (v, w), where v, w in V.
• Vertices are sometimes called nodes and
edges are sometimes called arcs.
• If the edge pair is ordered, the graph called a
directed graph.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-7
Definitions (2)
• Directed graphs are sometimes called
digraphs.
• In a digraph, vertex w is adjacent to vertex
v if and only if (v, w) ε E.
• Sometimes an edge has a third component
or called the edge cost (or weight) that
measures the cost of traversing the edge.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-8


Definitions (3)
The graph shown in Figure 14.1 has seven vertices,
V = {V0, V1, V2, V3, V4, V5, V6} and 12 edges.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-9


Definitions (4)

• The following vertices are adjacent to V3:


V2, V4, V5, and V6. Note that V0 and V1 are
not adjacent to V3. For this graph, |V| = 7
and |E| = 12. |S| represents the size of set S.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-10
Definitions (5)
• A path in a graph is a sequence of vertices
connected by edges.
• The path length is the number of edges on
the path (N-1) also called the unweighted
path length.
• The weighted path length is the sum of the
costs of the edges on the path. For example,
V0, V3, V5 is a path from vertex V0 to V5.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-11


Definitions (6)
• The path length is two edges (the shortest
path between V0 and V5) and the weighted
path length is 9.
• However, if cost important, the weighted
shortest path between these vertices has
cost 6 and is V0, V3, V6, V5.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-12


Definitions (7)
• A path may exist from a vertex to itself. If
this path contains no edges, the path length
is 0, which is a convenient way to define an
otherwise special case.
• A simple path is a path in which all
vertices are distinct, except that the first and
last vertices can be the same.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-13


Definitions (8)
• A cycle in a directed graph is a path that
begins and ends at the same vertex and
contains at least one edge. That is, it has a
length of at least 1 such that w1 = wN.
• The cycle is simple if the path is simple.
• A Directed Acyclic Graph -DAG is a type
of directed graph having no cycles.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-14


Directed Cyclic Graph

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-15


Directed Acyclic Graphs

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-16


Definitions (9)
• An example of a real-life situation that can
be modeled by a graph is an airport system.
• Each airport is a vertex. If there is a nonstop
flight between two airports, two vertices are
connected by an edge. The edge could have
a weight representing time, distance, or the
cost of the flight.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-17


Definitions (10)
• In an undirected graph, an edge (v, w)
would imply an edge (w, v).
• However, the costs of the edges might be
different because flying different directions
might take longer (depending on prevailing
winds) or cost more (depending on local
taxes).

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-18


Definitions (11)
• Thus we use a directed graph with both
edges listed, possibly with different
weights.
• To determine quickly the best flight
between any two airports, best could mean
the path with the fewest edges or one, or all,
of the weight measures (distance, cost, and
so on).
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-19
Definitions (12)
• A second example of a real-life situation that
can be modeled by a graph is the routing of
electronic mail through computer networks.
• Vertices represent computers, edges
represent links between pairs of computers,
and the edge costs represent communication
costs (phone bill per megabyte), delay costs
(seconds per megabyte), or combinations of
these and other factors.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-20


Sparse Graph
• For most graphs, there is likely at most one
edge from any vertex v to any. In most
applications, however, a sparse graph is
the norm.
• For instance, in the airport model, we do
not expect direct flights between every pair
of airports.
• Instead, a few airports are very well
connected and most others have relatively
few flights.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-21
Graph Representation

• Assume that the vertices are sequentially


numbered starting from 0.
• One simple way to represent a graph is to
use a two-dimensional array called an
adjacency matrix.
http://oneweb.utc.edu/~Christopher-Mawata/petersen/lesson7.htm

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-22


Adjacency Matrix

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-23


Labeled Graph/Adjacency Matrix

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-24


Graph Representation (2)
• For each edge (v, w), we set a[v] [w] equal
to the edge cost.
• Nonexistent edges can be initialized with a
logical INFINITY.
• The initialization of the graph seems to
require that the entire adjacency matrix be
initialized to INFINITY.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-25


Graph Representation (3)

• Then, as an edge is encountered, an appropriate


entry is set. In this scenario, the initialization takes |
E| ≤|V|2) time.
• When most edges are present, we have |E|= Θ|V|2 .
• Such a graph is considered to be a dense graph, it
has a large number of edges, generally quadratic.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-26


Adjacency List

• For sparse graphs, a better solution is an


adjacency list, which represents a graph by
using linear space.
• For each vertex, we keep a list of all
adjacent vertices. An adjacency list
representation of the graph in Figure 14.1
using a linked list is shown in Figure 14.2.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-27


Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-28
Adjacency List (2)
• The adjacency list can be constructed in
linear time from a list of edges.
• We begin by making all the lists empty.
When we encounter an edge (v, w, cv,w), we
add an entry consisting of w and the cost
cv,w to v's adjacency list.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-29


Adjacency List (3)
• Note that when inserting an edge, we do
not check whether it is already present.
• That cannot be done in constant time (using
a simple linked list), and doing the check
would destroy the linear-time bound for
construction.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-30


Shortest Path Calculation
• Figure 14.4 shows the representation in
which we use internal numbers.
• Figure 14.5 replaces the internal numbers
with Vertex variables, as we do in our
code.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-31


Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-32
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-33

You might also like