Chapter 7

Hail
of
University
ICS 202

2011 spring

Data Structures and Algorithms

1
of
1. Basics
2. Implementing Graphs
University
Hail
Outline
3. Graph Traversals
4. Shortest-Path Algorithms
5. Minimum-Cost Spanning Trees
6. Application: Critical Path Analysis
2
University
of
Hail
Lec 1
University
of
Hail
1. Basics
4
Definition (Directed Graph) A directed graph, or digraph , is an ordered
pair
with the following properties:
University
of
Hail
1. Basics: Directed Graphs
1.The first component, , is a finite, non-empty set. The elements of
are
called the vertices of G.
2.The second component,
That is,
, is a finite set of ordered pairs of vertices.
. The elements of
are called the edges of G.
5
University
of
Hail
1. Basics: Directed Graphs
For example, consider the directed graph
comprised of four
vertices(nodes) and six edges.
6
1. Basics: Directed Graphs
University
of
Hail
Terminology
Consider a directed graph
• Each element of
.
is called a vertex or a node of G. Hence,
is the set of
vertices (or nodes) of G.
• Each element of
is called an edge or an arc of G. Hence,
is the set of
edges (or arcs) of G.
• An edge
can be represented as
.
An arrow that points from
v to w is known as a directed arc. Vertex w is called the head of the arc.
Conversely, v is called the tail of the arc. Finally, vertex w is said to be
adjacent to (next) vertex v.
• An edge e=(v,w) is said to emanate (begin)
notation
from vertex v. We use
to denote the set of edges emanating from vertex v. That is,
7
University
of
Hail
1. Basics: Directed Graphs
• The out-degree of a node is the number of edges emanating from that node.
Therefore, the out-degree of v is
• An edge e=(v,w) is said to be incident on vertex w. We use notation
denote
the
set
of
edges
incident
on
vertex
w.
to
That
is,
• The in-degree of a node is the number of edges incident on that node.
Therefore, the in-degree of w is
8
University
of
Hail
1. Basics: Directed Graphs
vertex v
out-degree
in-degree
a
2
1
b
1
1
c
2
2
d
1
2
9
University
of
Hail
1. Basics: Directed Graphs
Definition (Path and Path Length)
A path in a directed graph
where
for
is a non-empty sequence of vertices
such that
for
.
The length of path P is k-1.
10
1. Basics: Directed Graphs
University
of
Hail
More Terminology
Consider the path
• Vertex
in a directed graph
is the successor of vertex
for
.
. Each element
of path
P (except the last) has a successor.
• Vertex
is the predecessor of vertex
for
. Each element
of
path P (except the first) has a predecessor.
• A path P is called a simple path if and only if
that
as
. However, it is permissible (acceptable) for
for all i and j such
to be the same
in a simple path.
11
More Terminology
Consider the path
in a directed graph
.
University
of
Hail
1. Basics: Directed Graphs
• A cycle is a path P of non-zero length in which
. The length of a cycle
is just the length of the path P.
• A loop is a cycle of length one. That is, it is a path of the form
.
• A simple cycle is a path that is both a cycle and simple.
12
1. Basics: Directed Graphs
 For certain applications it is convenient to deal with graphs that contain no
cycles. For example, a tree is a special kind of graph that contains no cycles.
University
of
Hail
Directed Acyclic Graphs
Definition (Directed Acyclic Graph (DAG))
 A directed, acyclic graph is a directed graph that contains no cycles.
 Obviously, all trees are DAGs. However, not all DAGs are trees
13
University
of
Hail
1. Basics: Directed Graphs
G2 is a tree and is a DAGs. However, not all DAGs are trees
G3 is DAGs but not a trees
14
1. Basics: Undirected Graphs
University
of
Hail
Undirected Graphs
An undirected graph is a graph in which the nodes are connected by undirected
arcs. An undirected arc is an edge that has no arrow. Both ends of an
undirected arc are equivalent - there is no head or tail. Therefore, we represent
an edge in an undirected graph as a set rather than an ordered pair:
Definition (Undirected Graph)
pair
An undirected graph is an ordered
with the following properties:
1. The first component,
, is a finite, non-empty set. The elements of
are
called the vertices of G.
2. The second component,
, is a finite set of sets. Each element of
is a
set that is comprised of exactly two (distinct) vertices. The elements
of
are called the edges of G.
15
University
of
Hail
1. Basics: Undirected Graphs
consider the undirected graph
comprised of four vertices and four
edges:
16
University
of
Hail
1. Basics: Undirected Graphs
Terminology
Consider an undirected graph
• An edge
• The
emanates from and is incident on both vertices v and w.
set
of
set
is
edges
emanating
from
a
vertex
v
is
the
The set of edges incident on a vertex w
.
17
1. Basics: Labeled Graphs
University
of
Hail
Labeled Graphs
Practical applications of graphs usually require that they be annotated with
additional information. Such information may be attached to the edges of the
graph and to the nodes of the graph. A graph which has been annotated in
some way is called a labeled graph
18
University
of
Hail
1. Basics: Representing Graphs
Representing Graphs
 Consider a directed graph
most
. Since
,graph G contains at
edges.
 There are
possible sets of edges for a given set of vertices .
 The main concern is to find a suitable way to represent the set of edges.
19
Hail
simplest graph representation scheme uses an
University
Adjacency Matrices
of
1. Basics: Undirected Graphs
 Consider a directed graph
with n vertices,
. The
matrix A of 0’s and 1’s
given by
 That is, the
element of the matrix, is a 1 only if
is an edge in G.
 The matrix A is called an adjacency matrix
20
University
of
Hail
Lec 2
University
of
Hail
2. Implementing Graphs
22
public interface Vertex extends Comparable
{
int getNumber (); // returns the number of a vertex
of
Hail
2. Implementing Graphs: Vertices
University
Object getWeight (); // returns an object representing the weight of a vertex
Enumeration getIncidentEdges (); // enumerates the edges incident to a vertex
(in)
Enumeration getEmanatingEdges (); // enumerates the edges emanating from a vertex(out)
Enumeration getPredecessors (); // enumerates the vertices predecessors of a vertex
Enumeration getSuccessors (); // enumerates the vertices successors of a vertex
}
23
public interface Edge extends Comparable
{
// an edge connects two vertices, v0 and v1
Vertex getV0 ();
of
Hail
2. Implementing Graphs: Edges
University
Vertex getV1 ();
Object getWeight (); // returns an object representing the weight of the edge
boolean isDirected ();
Vertex getMate (Vertex vertex); // given a vertex of an edge get Mate returns the
// other vertex of the same edge
}
24
 For every instance e of a class that implements the Edge interface, the
getMate method satisfies the following identities:
University
of
Hail
2. Implementing Graphs: Edges
 If we know that a vertex v is one of the vertices of e, then we can find the
other vertex by calling e.getMate(v).
25
University
of
Hail
2. Implementing Graphs: Graphs and Digraphs
public interface Graph extends Container
{
int getNumberOfEdges ();
int getNumberOfVertices ();
boolean isDirected ();
void addVertex (int v); // inserts a vertex into a graph
void addVertex (int v, Object weight); // insert a weighted vertex
Vertex getVertex (int v); // returns the vth vertex in the graph
void addEdge (int v, int w);
void addEdge (int v, int w, Object weight);
Edge getEdge (int v, int w);
boolean isEdge (int v, int w); // returns true if the graph contains the edge (v, w)
boolean isConnected ();
boolean isCyclic ();
Enumeration getVertices (); // return the vertices of the graph
Enumeration getEdges (); // return the edges of the graph
void depthFirstTraversal (PrePostVisitor visitor, int start);
void breadthFirstTraversal (Visitor visitor, int start);
}
26
University
of
Hail
2. Implementing Graphs: Directed Graphs
public interface Digraph extends Graph
{
boolean isStronglyConnected ();
void topologicalOrderTraversal (Visitor visitor);
}
27
University
of
Hail
2. Implementing Graphs: Abstract Graphs
public abstract class AbstractGraph extends AbstractContainer implements Graph
{
protected int numberOfVertices;
protected int numberOfEdges;
protected Vertex[] vertex;
public AbstractGraph (int size)
{
vertex = new Vertex [size];
}
protected final class GraphVertex extends AbstractObject implements Vertex
{
protected int number;
protected Object weight;
}
protected final class GraphEdge extends AbstractObject implements Edge
{
protected int v0;
protected int v1;
protected Object weight;
}
protected abstract Enumeration getIncidentEdges (int v);
protected abstract Enumeration getEmanatingEdges (int v);
}
28
Graphs
public class GraphAsMatrix extends AbstractGraph
{
protected Edge[][] matrix; // matrix of edges
of
Hail
2. Implementing Graphs: Implementing Undirected
University
public GraphAsMatrix (int size) // size represents the maximum number
{
// of vertices of the graph
super (size);
matrix = new Edge [size][size];
}
}
29
Graphs
public class GraphAsLists extends AbstractGraph
{
protected LinkedList[] adjacencyList; // set of edges
of
Hail
2. Implementing Graphs: Implementing Undirected
public GraphAsLists (int size) // size is the maximum number of vertices
University
{
super (size);
adjacencyList = new LinkedList [size];
for (int i = 0; i < size; ++i)
adjacencyList [i] = new LinkedList ();
}
}
30
3. Graph Traversals: Depth-First Traversal
public abstract class AbstractGraph extends AbstractContainer implements Graph
Hail
{
protected int numberOfVertices;
protected int numberOfEdges;
of
protected Vertex[] vertex;
University
public void depthFirstTraversal ( PrePostVisitor visitor, int start)
{
boolean[] visited = new boolean [numberOfVertices];
for (int v = 0; v < numberOfVertices; ++v)
visited [v] = false;
depthFirstTraversal (visitor, vertex [start], visited);
}
// ...
}
31
3. Graph Traversals: Depth-First Traversal
public abstract class AbstractGraph extends AbstractContainer implements Graph
{
Hail
protected int numberOfVertices;
protected int numberOfEdges;
protected Vertex[] vertex;
private void depthFirstTraversal ( PrePostVisitor visitor, Vertex v, boolean[] visited)
of
{
University
if (visitor.isDone ())
return;
visitor.preVisit (v);
visited [v.getNumber ()] = true;
Enumeration p = v.getSuccessors ();
while (p.hasMoreElements ())
{
Vertex to = (Vertex) p.nextElement ();
if (!visited [to.getNumber ()])
depthFirstTraversal (visitor, to, visited);
}
visitor.postVisit (v);
}
}
32
University
of
Hail
3. Graph Traversals: Breadth-First Traversal
public abstract class AbstractGraph extends AbstractContainer implements Graph
{
protected int numberOfVertices;
protected int numberOfEdges;
protected Vertex[] vertex;
public void breadthFirstTraversal ( Visitor visitor, int start)
{
boolean[] enqueued = new boolean [numberOfVertices];
for (int v = 0; v < numberOfVertices; ++v)
enqueued [v] = false;
Queue queue = new QueueAsLinkedList ();
enqueued [start] = true;
queue.enqueue (vertex [start]);
while (!queue.isEmpty () && !visitor.isDone ())
{
Vertex v = (Vertex) queue.dequeue ();
visitor.visit (v);
Enumeration p = v.getSuccessors ();
while (p.hasMoreElements ())
{
Vertex to = (Vertex) p.nextElement ();
if (!enqueued [to.getNumber ()])
{
enqueued [to.getNumber ()] = true;
queue.enqueue (to);
}
}
}
}
}
33
University
of
Hail
3. Graph Traversals: Breadth-First Traversal
34
Hail
Definition (Topological Sort)
of
Consider a directed acyclic graph
University
3. Graph Traversals: Topological Sort
of G is a sequence
. A topological sort of the vertices
in which each element of
exactly once. For every pair of distinct vertices
if
is an edge in G, i.e.,
and
appears
in the sequence S,
, then i < j.
35
University
of
Hail
3. Graph Traversals: Topological Sort
36
3. Graph Traversals: Topological Sort
Hail
 Clearly the first vertex in a topological sort must have in-degree zero
of
and every DAG must contain at least one vertex with in-degree zero.
University
 To find a topological sort, we consider the in-degrees of the vertices.
 A simple algorithm to create the sort goes like this: Repeat the
following steps until the graph is empty:
• Select a vertex that has in-degree zero.
• Add the vertex to the sort.
• Delete the vertex and all the edges emanating from it from the graph.
37
University
of
Hail
3. Graph Traversals: Topological Sort
public abstract class AbstractGraph extends AbstractContainer implements Graph
{
protected int numberOfVertices;
protected int numberOfEdges;
protected Vertex[] vertex;
public void topologicalOrderTraversal (Visitor visitor)
{
int[] inDegree = new int [numberOfVertices];
for (int v = 0; v < numberOfVertices; ++v)
inDegree [v] = 0;
Enumeration p = getEdges ();
while (p.hasMoreElements ()) // calculate the indegrees of all the vertices
{
Edge edge = (Edge) p.nextElement ();
Vertex to = edge.getV1 ();
++inDegree [to.getNumber ()];
}
Queue queue = new QueueAsLinkedList ();
for (int v = 0; v < numberOfVertices; ++v)
if (inDegree [v] == 0)
queue.enqueue (vertex [v]);
while (!queue.isEmpty () && !visitor.isDone ())
{
Vertex v = (Vertex) queue.dequeue ();
visitor.visit (v);
Enumeration q = v.getSuccessors ();
while (q.hasMoreElements ())
{
Vertex to = (Vertex) q.nextElement ();
if (--inDegree [to.getNumber ()] == 0)
queue.enqueue (to);
}
}
}
}
38
Hail
An undirected graph
University
Definition (Connectedness of an Undirected Graph)
of
3. Graph Traversals: Connectedness of an Undirected Graph
between every pair of vertices in
is connected if there is a path in G
.
39
University
of
Hail
3. Graph Traversals: Connectedness of an Undirected Graph
Clearly, the graph
is not connected
40
3. Graph Traversals: Connectedness of an Undirected Graph
public abstract class AbstractGraph extends AbstractContainer implements Graph
{
University
of
Hail
protected int numberOfVertices;
protected int numberOfEdges;
protected Vertex[] vertex;
protected final static class Counter
{
int value = 0;
}
public boolean isConnected ()
{
final Counter counter = new Counter ();
PrePostVisitor visitor = new AbstractPrePostVisitor ()
{
public void visit (Object object)
{
++counter.value;
}
};
depthFirstTraversal (visitor, 0);
return counter.value == numberOfVertices;
}
}
41
 When dealing with directed graphs, we define two kinds of
connectedness, strong and weak.
University
of
Hail
3. Graph Traversals: Connectedness of a Directed Graph
Definition (Strong Connectedness of a Directed Graph)
A directed graph
is strongly connected if there is a path in G
between every pair of vertices in
.
42
University
of
Hail
3. Graph Traversals: Connectedness of a Directed Graph
Definition (Weak Connectedness of a Directed Graph)
A directed graph
graph
is weakly connected if the underlying undirected
is connected.
43
University
of
Hail
3. Graph Traversals: Connectedness of a Directed Graph
public abstract class AbstractGraph extends AbstractContainer implements Graph
{
protected int numberOfVertices;
protected int numberOfEdges;
protected Vertex[] vertex;
public boolean isStronglyConnected ()
{
final Counter counter = new Counter ();
for (int v = 0; v < numberOfVertices; ++v)
{
counter.value = 0;
PrePostVisitor visitor = new AbstractPrePostVisitor()
{
public void visit (Object object)
{
++counter.value;
}
};
depthFirstTraversal (visitor, v);
if (counter.value != numberOfVertices)
return false;
}
return true; // if all the vertices are visited in each traversal
}
}
44
3. Graph Traversals: Testing for Cycles in a Directed Graph
 An easy way to test if a directed graph is cyclic is to attempt a topological-order
Hail
 This traversal only visits all the vertices of a directed graph if that graph contains no
of
degrees of the vertices. (The number shown below each vertex).
University
traversal
 After a vertex is visited, the vertex and all the edges emanating from that vertex are
cycles.
 Example (G10): the topological traversal algorithm begins by computing the in-
 At each step of the traversal, a vertex with in-degree of zero is visited.
removed from the graph.
 Notice that if we remove vertex a and edge (a,b) from G10, all the remaining vertices
have in-degrees of one. The presence of the cycle prevents the topological-order
traversal from completing.
45
3. Graph Traversals: Testing for Cycles in a Directed Graph
public abstract class AbstractGraph extends AbstractContainer implements Graph
{
University
of
Hail
protected int numberOfVertices;
protected int numberOfEdges;
protected Vertex[] vertex;
public boolean isCyclic ()
{
final Counter counter = new Counter ();
Visitor visitor = new AbstractVisitor ()
{
public void visit (Object object)
{
++counter.value;
}
};
topologicalOrderTraversal (visitor);
return counter.value != numberOfVertices;
}
}
46