U n i v e r s i t y o f H a i l ICS 202  2011 spring  Data Structures and Algorithms  1.

Slides:



Advertisements
Similar presentations
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Advertisements

Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graph & BFS.
Topological Sort Introduction. Definition of Topological Sort.
Testing for Connectedness and Cycles
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
1 Testing for Connectedness and Cycles Connectedness of Undirected Graphs Implementation of Connectedness detection Algorithm for Undirected Graphs. Implementation.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Introduction to Graphs
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
1 Graph Introduction Definitions. 2 Definitions I zDirected Graph (or Di-Graph) is an ordered pair G=(V,E) such that yV is a finite, non-empty set (of.
Graph Traversals Depth-First Traversal. The Algorithm.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Implementation of Graphs Identification of Classes. The Vertex Interface. The Edge Interface. Graph and Digraph Interfaces. Two concrete Implementations.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
1 Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithms. An Example. Implementation.
Testing for Connectedness & Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong Connectedness.
Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong.
Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Data Structures and Algorithms Graphs. 2Content MotivationMotivation GraphGraph –Directed Graph –Undirected Graph –Representation –Implementation AlgorithmsAlgorithms.
GRAPH Learning Outcomes Students should be able to:
Data Structures Using C++ 2E
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Data Structures and Algorithms Graphs Fall 2006 Major part of this presentation is courtesy of Dr.Bingol.
Chapter 2 Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
COSC 2007 Data Structures II Chapter 14 Graphs I.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
Graphs Upon completion you will be able to:
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Chapter 9: Graphs.
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
abstract data types built on other ADTs
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Chapter 13: The Graph Abstract Data Type
Introduction to Graphs
Introduction to Graphs
Graph Traversals Depth-First Traversals. Algorithms. Example.
Testing for Connectedness and Cycles
Topological Sort Introduction. Definition of Topological Sort.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Topological Sort Introduction. Definition of Topological Sort.
Topological Sort Introduction. Definition of Topological Sort.
Topological Sort Introduction. Definition of Topological Sort.
Introduction to Graphs
Introduction to Graphs
Presentation transcript:

U n i v e r s i t y o f H a i l ICS 202  2011 spring  Data Structures and Algorithms  1

U n i v e r s i t y o f H a i l 2 Outline 1.Basics 2.Implementing Graphs 3.Graph Traversals 4.Shortest-Path Algorithms 5.Minimum-Cost Spanning Trees 6.Application: Critical Path Analysis

U n i v e r s i t y o f H a i l Lec 1

U n i v e r s i t y o f H a i l 4 1. Basics

U n i v e r s i t y o f H a i l 5 1. Basics: Directed Graphs Definition (Directed Graph) A directed graph, or digraph, is an ordered pair 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 ordered pairs of vertices. That is,. The elements of are called the edges of G.

U n i v e r s i t y o f H a i l 6 1. Basics: Directed Graphs For example, consider the directed graph comprised of four vertices(nodes) and six edges.

U n i v e r s i t y o f H a i l 7 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,

U n i v e r s i t y o f H a i l 8 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

U n i v e r s i t y o f H a i l 9 1. Basics: Directed Graphs vertex v out-degree in-degree a 2 1 b 1 1 c 2 2 d 1 2

U n i v e r s i t y o f H a i l 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.

U n i v e r s i t y o f H a i l 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.

U n i v e r s i t y o f H a i l 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.

U n i v e r s i t y o f H a i l 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

U n i v e r s i t y o f H a i l Basics: Directed Graphs G2 is a tree and is a DAGs. However, not all DAGs are trees G3 is DAGs but not a trees

U n i v e r s i t y o f H a i l 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: Definition (Undirected Graph) An undirected graph is an ordered pair 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.

U n i v e r s i t y o f H a i l Basics: Undirected Graphs consider the undirected graph comprised of four vertices and four edges:

U n i v e r s i t y o f H a i l 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.

U n i v e r s i t y o f H a i l 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

U n i v e r s i t y o f H a i l Basics: Representing Graphs 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.  The main concern is to find a suitable way to represent the set of edges.

U n i v e r s i t y o f H a i l Basics: Undirected Graphs 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.  The matrix A is called an adjacency matrix

U n i v e r s i t y o f H a i l Lec 2

U n i v e r s i t y o f H a i l Implementing Graphs

U n i v e r s i t y o f H a i l 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 }

U n i v e r s i t y o f H a i l 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 }

U n i v e r s i t y o f H a i l 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).

U n i v e r s i t y o f H a i l 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); }

U n i v e r s i t y o f H a i l Implementing Graphs: Directed Graphs public interface Digraph extends Graph { boolean isStronglyConnected (); void topologicalOrderTraversal (Visitor visitor); }

U n i v e r s i t y o f H a i l 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); }

U n i v e r s i t y o f H a i l 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]; }

U n i v e r s i t y o f H a i l 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 (); }

U n i v e r s i t y o f H a i l 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); } //... }

U n i v e r s i t y o f H a i l 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); }

U n i v e r s i t y o f H a i l 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); }

U n i v e r s i t y o f H a i l Graph Traversals: Breadth-First Traversal

U n i v e r s i t y o f H a i l Graph Traversals: Topological Sort 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.

U n i v e r s i t y o f H a i l Graph Traversals: Topological Sort

U n i v e r s i t y o f H a i l 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.

U n i v e r s i t y o f H a i l 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); }

U n i v e r s i t y o f H a i l Graph Traversals: Connectedness of an Undirected Graph Definition (Connectedness of an Undirected Graph) An undirected graph is connected if there is a path in G between every pair of vertices in.

U n i v e r s i t y o f H a i l Graph Traversals: Connectedness of an Undirected Graph Clearly, the graph is not connected

U n i v e r s i t y o f H a i l 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 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; }

U n i v e r s i t y o f H a i l Graph Traversals: Connectedness of a Directed Graph  When dealing with directed graphs, we define two kinds of connectedness, strong and weak. 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.

U n i v e r s i t y o f H a i l 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.

U n i v e r s i t y o f H a i l 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 }

U n i v e r s i t y o f H a i l 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.

U n i v e r s i t y o f H a i l 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; }