500 likes | 743 Views
Chapter 7 Graphs and Graph Algorithms. Outline. Basics Implementing Graphs Graph Traversals Shortest-Path Algorithms Minimum-Cost Spanning Trees Application: Critical Path Analysis. L ec 1. 1. Basics. 1. Basics: Directed Graphs.
E N D
Chapter 7Graphs and Graph Algorithms ICS 202 2011 spring Data Structures and Algorithms
Outline • Basics • Implementing Graphs • Graph Traversals • Shortest-Path Algorithms • Minimum-Cost Spanning Trees • Application: Critical Path Analysis
1. Basics: Directed Graphs • Definition (Directed Graph) A directed graph, or digraph , is an ordered pair with the following properties: • The first component, , is a finite, non-empty set. The elements of are called the vertices of G. • The second component, , is a finite set of ordered pairs of vertices. That is, . The elements of are called the edges of G.
1. Basics: Directed Graphs For example, consider the directed graph comprised of four vertices(nodes) and six edges.
1. Basics: Directed Graphs • 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) from vertex v. We use notation to denote the set of edges emanating from vertex v. That is,
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 to denote the set of edges incident on vertex w. That is, • The in-degree of a node is the number of edges incident on that node. Therefore, the in-degree of w is
1. Basics: Directed Graphs Definition (Path and Path Length) A path in a directed graph is a non-empty sequence of vertices where for such that for . The length of path P is k-1.
1. Basics: Directed Graphs • More Terminology • Consider the path in a directed graph . • Vertex 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 for all i and j such that . However, it is permissible (acceptable) for to be the same as in a simple path.
1. Basics: Directed Graphs • More Terminology • Consider the path in a directed graph . • 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.
1. Basics: Directed Graphs • Directed Acyclic 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. • 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
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
Definition (Undirected Graph) An undirected graph is an ordered pair with the following properties: • The first component, , is a finite, non-empty set. The elements of are called the vertices of G. • 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. 1. Basics: Undirected Graphs 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:
1. Basics: Undirected Graphs consider the undirected graph comprised of four vertices and four edges:
1. Basics: Undirected Graphs • Terminology • Consider an undirected graph • An edge emanates from and is incident on both vertices v and w. • The set of edges emanating from a vertex v is the set The set of edges incident on a vertex w is .
1. Basics: Labeled Graphs 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
Representing Graphs • Consider a directed graph . Since ,graph G contains at most edges. • There are possible sets of edges for a given set of vertices . 1. Basics: Representing Graphs • The main concern is to find a suitable way to represent the set of edges.
Adjacency Matrices • Consider a directed graph with n vertices, . The simplest graph representation scheme uses an 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. 1. Basics: Undirected Graphs • The matrix A is called an adjacency matrix
2. Implementing Graphs: Vertices public interface Vertex extends Comparable { int getNumber (); // returns the number of a vertex 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 }
2. Implementing Graphs: Edges public interface Edge extends Comparable { // an edge connects two vertices, v0 and v1 Vertex getV0 (); 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 }
2. Implementing Graphs: Edges • For every instance e of a class that implements the Edge interface, the getMate method satisfies the following identities: • 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).
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); }
2. Implementing Graphs: Directed Graphs public interface Digraph extends Graph { boolean isStronglyConnected (); void topologicalOrderTraversal (Visitor visitor); }
2. Implementing Graphs: Abstract Graphs public abstract class AbstractGraph extends AbstractContainer implements Graph { protectedint numberOfVertices; protectedint 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); }
2. Implementing Graphs: Implementing Undirected Graphs public class GraphAsMatrix extends AbstractGraph { protected Edge[][] matrix; // matrix of edges public GraphAsMatrix (int size) // size represents the maximum number { // of vertices of the graph super (size); matrix = new Edge [size][size]; } }
2. Implementing Graphs: Implementing Undirected Graphs public class GraphAsLists extends AbstractGraph { protected LinkedList[] adjacencyList; // set of edges public GraphAsLists (int size) // size is the maximum number of vertices { super (size); adjacencyList = new LinkedList [size]; for (int i = 0; i < size; ++i) adjacencyList [i] = new LinkedList (); } }
3. Graph Traversals: Depth-First Traversal public abstract class AbstractGraph extends AbstractContainer implements Graph { protected int numberOfVertices; protected int numberOfEdges; protected Vertex[] vertex; 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); } // ... }
3. Graph Traversals: Depth-First Traversal public abstract class AbstractGraph extends AbstractContainer implements Graph { protected int numberOfVertices; protected int numberOfEdges; protected Vertex[] vertex; private void depthFirstTraversal ( PrePostVisitor visitor, Vertex v, boolean[] visited) { 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); } }
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 = newboolean [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); } } } } }
Definition (Topological Sort) Consider a directed acyclic graph . A topological sort of the vertices of G is a sequence in which each element of appears exactly once. For every pair of distinct vertices and in the sequence S, if is an edge in G, i.e., , then i < j. 3. Graph Traversals: Topological Sort
3. Graph Traversals: Topological Sort • To find a topological sort, we consider the in-degrees of the vertices. • Clearly the first vertex in a topological sort must have in-degree zero and every DAG must contain at least one vertex with in-degree zero. • 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.
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); } } } }
Definition (Connectedness of an Undirected Graph) An undirected graph is connected if there is a path in G between every pair of vertices in . 3. Graph Traversals: Connectedness of an Undirected Graph
3. Graph Traversals: Connectedness of an Undirected Graph Clearly, the graph is not connected
3. Graph Traversals: Connectedness of an Undirected Graph public abstract class AbstractGraph extends AbstractContainer implements Graph { protected int numberOfVertices; protected int numberOfEdges; protected Vertex[] vertex; protected final staticclass 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; } }
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 . 3. Graph Traversals: Connectedness of a Directed Graph • When dealing with directed graphs, we define two kinds of connectedness, strong and weak.
3. Graph Traversals: Connectedness of a Directed Graph Definition (Weak Connectedness of a Directed Graph) A directed graph is weakly connected if the underlying undirected graph is connected.
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 } }
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 traversal • This traversal only visits all the vertices of a directed graph if that graph contains no cycles. • Example (G10): the topological traversal algorithm begins by computing the in-degrees of the vertices. (The number shown below each vertex). • At each step of the traversal, a vertex with in-degree of zero is visited. • After a vertex is visited, the vertex and all the edges emanating from that vertex are 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.
3. Graph Traversals: Testing for Cycles in a Directed Graph public abstract class AbstractGraph extends AbstractContainer implements Graph { 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; } }