1 Graphs & Characteristics Graph Representations A Representation in C++ (Ford & Topp) Searching (DFS & BFS) Connected Components Graph G and Its Transpose.

Slides:



Advertisements
Similar presentations
CSE Lectures 18 – Graphs Graphs & Characteristics
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Algoritma Traversal Graph. Graph Traversal Algorithms In general, graphs do not have a vertex, like a root, that initiates unique paths to each of the.
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
Graph & BFS.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 25 Graph.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Chapter 2 Graph Algorithms.
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,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Data Structures & Algorithms Graphs. Graph Terminology A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices.
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.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graphs Upon completion you will be able to:
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
Chapter 05 Introduction to Graph And Search Algorithms.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Graphs Representation, BFS, DFS
Podcast Ch26a Title: Representing Graphs
CS120 Graphs.
Graphs Graph transversals.
Podcast Ch25d Title: Minimum Path Algorithm
Graph Implementation.
Chapter 16 1 – Graphs Graph Categories Strong Components
CSE 417: Algorithms and Computational Complexity
Podcast Ch26b Title: Digraph Class Implementation
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

1 Graphs & Characteristics Graph Representations A Representation in C++ (Ford & Topp) Searching (DFS & BFS) Connected Components Graph G and Its Transpose G T Shortest-Path Example Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 18 & 19 – Graphs

2 What is a graph? Formally, a graph G(V,E) is A set of vertices V A set of edges E, such that each edge e i,j connects two vertices v i and v j in V V and E may be empty

3 Graph Categories A graph is connected if each pair of vertices have a path between them A complete graph is a connected graph in which each pair of vertices are linked by an edge

4 Example of Digraph Graphs with ordered edges are called directed graphs or digraphs

5 Strength of Connectedness (digraphs only) Strongly connected if there is a path from each vertex to every other vertex. A C B E D (a) Not Strongly or Weakly Connected (No pathE to D or D toE) C A ED B (b) Strongly Connected

6 Strength of Connectedness (digraphs only) Weakly connected if, for each pair of vertices v i and v j, there is either a path P(v i, v j ) or a path P(v i, v j ). A C B E D A D C B (a) (c) Not Strongly or Weakly Connected (No pathE to D or D toE) Weakly Connected (No path from D to a vertex)

7 Representing Graphs Adjacency Matrix Edges are represented in a 2-D matrix Adjacency Set (or Adjacency List) Each vertex has an associated set or list of edges leaving Edge List The entire edge set for the graph is in one list Mentioned in discrete math (probably)

8 Adjacency Matrix An m x m matrix, called an adjacency matrix, identifies graph edges. An entry in row i and column j corresponds to the edge e = (v i, v j ). Its value is the weight of the edge, or -1 if the edge does not exist. D A C E B

9 Adjacency Set (or List) An adjacency set or adjacency list represents the edges in a graph by using … An m element map or vector of vertices Where each vertex has a set or list of neighbors Each neighbor is at the end of an out edge with a given weight (a) D A C E B A E D C B Vertices Set of Neighbors B1 C1 C1 B1 D1B1

10 Adjacency Matrix and Adjacency Set (side-by-side) D B A E C

11 Building a graph class Neighbor Identifies adjacent vertex and edge weight VertexInfo Contains all characteristics of a given vertex, either directly or through links VertexMap Contains names of vertices and links to the associated VertexInfo objects

12 vertexInfo Object A vertexInfo object consists of seven data members. The first two members, called vtxMapLoc and edges, identify the vertex in the map and its adjacency set.

13 vertexInfo object vtxMapLoc – iterator to vertex (name) in map edges – set of vInfo index / edge weight pairs Each is an OUT edge to an adjacent vertex vInfo[index] is vertexInfo object for adjacent vertex inDegree – # of edges coming into this vertex outDegree is simply edges.size() occupied – true (this vertex is in the graph), false (this vertex was removed from graph) color – (white, gray, black) status of vertex during search dataValue – value computed during search (distance from start, etc) parent – parent vertex in tree generated by search

14 A Neighbor class (edges to adjacent vertices) class neighbor { public: int dest; // index of destination vertex in vInfo vector int weight; // weight of this edge // constructor neighbor(int d=0, int c=0) : dest(d), weight(c) {} // operators to compare destination vertices friend bool operator<(const neighbor& lhs, const neighbor& rhs) { return lhs.dest < rhs.dest; } friend bool operator==(const neighbor& lhs, const neighbor& rhs) { return lhs.dest == rhs.dest; } };

15 vertexInfo object (items in vInfo vector) template class vertexInfo { public: enum vertexColor { WHITE, GRAY, BLACK }; map ::iterator vtxMapLoc; // to pair in map set edges; // edges to adjacent vertices int inDegree; // # of edges coming into vertex bool occupied; // currently used by vertex or not vertexColor color; // vertex status during search int dataValue; // relevant data values during search int parent; // parent in tree built by search // default constructor vertexInfo(): inDegree(0), occupied(true) {} // constructor with iterator pointing to vertex in map vertexInfo(map ::iterator iter) : vtxMapLoc(iter), inDegree(0), occupied(true) {} };

16 A graph using a vertexMap and vertexInfo vector Graph vertices are stored in a map, called vtxMap Each entry is a key, value pair The initial size of the vertexInfo vector is the number of vertices in the graph There is a 1-1 correspondence between an entry in the map and a vertexInfo entry in the vector vertex mIter (iterator location) index... index vtxMapLoc edges inDegree occupied color dataValue vtxMap parent vInfo

17 A graph class (just the private members) private: typedef map vertexMap; vertexMap vtxMap; // store vertex in a map with its name as the key // and the index of the corresponding vertexInfo // object in the vInfo vector as the value vector > vInfo; // list of vertexInfo objects for the vertices int numVertices; int numEdges; // current size (vertices and edges) of the graph stack availStack; // availability stack, stores unused vInfo indices

18 VtxMap and Vinfo Example A DC B

19 Find the location for vertexInfo of vertex with name v // uses vtxMap to obtain the index of v in vInfo. // this is a private helper function template int graph ::getvInfoIndex(const T& v) const { vertexMap::const_iterator iter; int pos; // find the vertex : the map entry with key v iter = vtxMap.find(v); if (iter == vtxMap.end()) pos = -1; // wasn’t in the map else pos = (*iter).second; // the index into vInfo return pos; }

20 Find in and out degree of v // return the number of edges entering v template int graph ::inDegree(const T& v) const { int pos=getvInfoIndex(v); if (pos != -1) return vInfo[pos].inDegree; else throw graphError("graph inDegree(): v not in the graph"); } // return the number of edges leaving v template int graph ::outDegree(const T& v) const { int pos=getvInfoIndex(v); if (pos != -1) return vInfo[pos].edges.size(); else throw graphError("graph outDegree(): v not in the graph"); }

21 Insert a vertex into graph template void graph ::insertVertex(const T& v) { int index; // attempt insertion, set vInfo index to 0 for now pair result = vtxMap.insert(vertexMap::value_type(v,0)); if (result.second) { // insertion into map succeeded if (!availStack.empty()) { // there is an available index index = availStack.top(); availStack.pop(); vInfo[index] = vertexInfo (result.first); } else { // vInfo is full, increase its size vInfo.push_back(vertexInfo (result.first)); index = numVertices; } (*result.first).second = index; // set map value to index numVertices++; // update size info } else throw graphError("graph insertVertex(): v in graph"); }

22 Insert an edge into graph // add the edge (v1,v2) with specified weight to the graph template void graph ::insertEdge(const T& v1, const T& v2, int w) { int pos1=getvInfoIndex(v1), pos2=getvInfoIndex(v2); if (pos1 == -1 || pos2 == -1) throw graphError("graph insertEdge(): v not in the graph"); else if (pos1 == pos2) throw graphError("graph insertEdge(): loops not allowed"); // insert edge (pos2,w) into edge set of vertex pos1 pair ::iterator, bool> result = vInfo[pos1].edges.insert(neighbor(pos2,w)); if (result.second) // it wasn’t already there { // update counts numEdges++; vInfo[pos2].inDegree++; }

23 Erase an edge from graph // erase edge (v1,v2) from the graph template void graph ::eraseEdge(const T& v1, const T& v2) { int pos1=getvInfoIndex(v1), pos2=getvInfoIndex(v2); if (pos1 == -1 || pos2 == -1) throw graphError("graph eraseEdge(): v not in the graph"); // find the edge to pos2 in the list of pos1 neighbors set ::iterator setIter; setIter = vInfo[pos1].edges.find(neighbor(pos2)); if (setIter != edgeSet.end()) { // found edge in set, so remove it & update counts vInfo[pos1].edges.erase(setIter); vInfo[pos2].inDegree--; numEdges--; } else throw graphError("graph eraseEdge(): edge not in graph"); }

24 Erase a vertex from graph (algorithm) Find index of vertex v in vInfo vector Remove vertex v from map Set vInfo[index].occupied to false Push index onto availableStack For every occupied vertex in vInfo Scan neighbor set for edge pointing back to v If edge found, erase it For each neighbor of v, decrease its inDegree by 1 Erase the edge set for vInfo[index]

25 End Lecture 18 Begin Lecture 19

26 Graph traversals & searches Breadth first search (BFS) Searches in expanding rings All vertices one edge away, then all vertices two edges away, etc Depth first search (DFS) Searches as deeply as possible until trapped, then backtracks and searches again Search backtracks when No more edges exit current vertex No more of unused out edges from current node go to vertices that have not been already discovered

27 Breadth First Search (traversal) Uses a queue to order search and a set to store visited vertices Start with all unvisited (white) vertices Push start vertex onto Q While Q is not empty Remove vertex V from Q Mark V as visited (black) Insert V into the visited set For each adjacent vertex (each neighbor) U If U is unvisited (white)  Mark it seen (gray) and push it onto Q Return visited set (vertices reached from start) Running time – O(V + E)

28 Breadth-First Search Algorithm

29 Breadth-First Search Algorithm (continued)

30 Breadth-First Search Algorithm

31 Depth first search (traversal) Emulates a postorder traversal, backtracking search Visits occur while backing out DfsVisit(V, checkCycles) If V is unvisited (white) Mark V as seen (gray) For each neighbor U of V  If U is unvisited (white)  DfsVisit(U,checkCycles)  Else if U is previously discovered (gray) && checkCycles  Throw exception (found cycle) Mark V as visited (black) Push V onto FRONT of dfsList

32 dfs() discovery and visiting 1/7 G D C B F E A 6/4 5/6 2/3 4/1 3/2 7/5 m/n == discovered/visited Back edge

33 Strong Components A strongly connected component of a graph G is a maximal set of vertices SC in G that are mutually accessible.

34 Graph G and Its Transpose G T The transpose has the same set of vertices V as graph G but a new edge set E T consisting of the edges of G but with the opposite direction.

35 Finding Strong Components Perform dfs() of graph G, creating dfsGList Create G T (transform of graph G) Color all vertices in G T white For each vertex V in G T taken in order of dfsGList If V is white Perform dfsVisit() of G T from V and create dfsGTList Append dfsGTList to component vector At end of process, the component vector contains a set of vertices for each strong component in the graph G Finding G T is O(V+E) and dfs() is O(V+E) So, finding strong components is also O(V+E)

36 G, G T and its Strong Components dfsGList: A B C E D G F dfsGTLists: {A C B}, {E}, {D F G}

37 Topological sort of acyclic graphs Important in determining precedence order in graphs representing scheduling of activities Dfs() produces a topological sort of the vertices in the graph, returning them in the dfsList Graph must be acyclic To show that dfs() performs a topological sort show that if a path exists from V to W then V always appears ahead of W in dfsList We examine the three colors W may have when first encountered in path …

38 Path(v,w) found, v must precede W in dfsList

39 Shortest-Path Example Shortest-path is a modified breadth-first search Path length is number of edges traversed and is stored in dataValue field of vertex at time of its discovery The parent field is set to the index of the parent at the same time Path is recovered in reverse, using parent fields

40 Shortest-Path Example (path from F to C) Start: visitQ = F:0:Fformat(vertex:dataValue:parent) Next: visitQ = D:1:F, E:1:F Next: visitQ = E:1:F, A:2:D Next: visitQ = A:2:D Next: visitQ = B:3:A, C:3:A Next: visitQ = C:3:A Finish: C found, path length = 3, path = F,D,A,C : parent( parent( parent(C) ) )

41 Minimum (weight) path – Dijkstra’s algorithm Uses priority queue containing identities of all fringe vertices and the length of the minimum path to each from the start Algorithm builds a tree of all minimum length paths from start Each vertex is either tree, fringe or unseen At each step The fringe vertex V with the minimum path is removed from priorityQ and added to the tree V’s non-tree neighbors U become fringe and the minimum path length is computed from start, thru V to U and is stored in U.dataValue, V is saved as U.parent and U is added to priorityQ Process stops when queue is empty, or chosen destination vertex is found

42 Dijkstra Minimum-Path Algorithm (Example A to D) PriQ:(A,0)Tree (vertices & path weight) (B,4)(C,11)(E,4)A,0 (E,4)(C,11)(C,10)(D,12)A,0B,4 (C,10)(C,11)(D,12)A,0B,4E,4 (C,11)(D,12)A,0B,4E,4C,10 (D,12) A,0B,4E,4C,10 empty A,0B,4E,4C,10D,12

43 Minimum Spanning Tree Prim’s Algorithm Spanning tree for graph with minimum TOTAL weight Minimum Spanning Tree may not be unique, but total weight is same value for all All vertices are either tree, fringe, or unseen Priority queue is used to hold fringe vertices and the minimum weight edge connecting each to the tree Put start vertex in priorityQ While priorityQ not empty The nearest vertex V is removed from the queue and added to the tree For each non-tree neighbor U of V if the edge V,U weight < current U.dataValue U.dataValue is set to weight of edge V,U U.parent is set to V push U:weight pair onto priority queue

44 Minimum Spanning Tree Example

45 Minimum Spanning Tree: Step 1 (edge A-B) A B C D A B 2 Spanning tree with vertices A, B minSpanTreeSize = 2, minTreeWeight = 2

46 Minimum Spanning Tree: Step 2 (Edge A-D) A B C D D A B Spanning tree with vertices A, B, D minSpanTreeSize = 3, minTreeWeight = 7 2 5

47 Minimum Spanning Tree: Step 3 (Edge D-C) A B C D C 7 D A B Spanning tree with vertices A, B, D, C minSpanTreeSize = 4, minTreeWeight =

48 Runtime Orders of Complexity Min Spanning Tree – O(V + E log 2 E) Min Path (Dijkstra) – O(V + E log 2 E) Strong Components – O(V + E) Dfs – O(V+E) BFS – O(V+E)

49 Graphs – Important Terms Vertex, edge, adjacency, path, cycle Directed (digraph), undirected Complete Connected (strongly, weakly, components) Searches (DFS, BFS) Shortest Path, Minimum Path Euler Path, Hamiltonian Path Minimum Spanning Tree

50 Searching Graphs Breadth-First Search, bfs() Locates all vertices reachable from a starting vertex Uses a queue in process Can be used to find the minimum distance from a starting vertex to an ending vertex in a graph.

51 Searching Graphs Depth-First Search, dfs() Produces a list of all graph vertices in the reverse order of their finishing times. Supported by a recursive depth-first visit function, dfsVisit() An algorithm can check to see whether a graph is acyclic (has no cycles) and can perform a topological sort of a directed acyclic graph (DAG) Forms the basis for an efficient algorithm that finds the strong components of a graph

52 Searching Graphs Dijkstra's algorithm (minimum path) Uses a priority queue to determine a path from a starting to an ending vertex, of minimum weight Prim's algorithm (minimum spanning tree) An extension of Dijkstra’s algorithm, which computes the minimum spanning tree of an undirected, connected graph.