CMSC 341 Graphs 3. Adjacency Table Implementation Uses table of size |V|  |V| where each entry (i,j) is boolean –TRUE if there is an edge from vertex.

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Chapter 9: Graphs Topological Sort
Reachability as Transitive Closure
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Topological Sort.
Data Structures Using C++
Topological Sort and Hashing
Minimum cost spanning tree and activity networks Data structure 2002/12/2.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
Data Structures Using C++
1 Graph Theory © Dave Bockus. 2 Adjacency Matrix
Representing Relations Using Matrices
Graph & BFS.
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.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
Connected Components, Directed Graphs, Topological Sort COMP171.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
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. 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 03 / 25 / 2009 Instructor: Michael Eckmann.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Important Problem Types and Fundamental Data Structures
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
CISC 235: Topic 11 Shortest Paths Algorithms. CISC 235 Topic 112 Outline Single-Source Shortest Paths Algorithm for Unweighted Graphs Algorithm for Weighted,
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.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
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.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
1 ELEC692 Fall 2004 Lecture 1b ELEC692 Lecture 1a Introduction to graph theory and algorithm.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
CMSC 380 Graph Traversals and Search. 2 Graph Traversals Graphs can be traversed breadth-first, depth- first, or by path length We need to specifically.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
CMSC 341 Graphs. 8/3/2007 UMBC CMSC 341 Graphs 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs – Adjacency Matrix
2IS80 Fundamentals of Informatics Fall 2015 Lecture 7: Sorting and Directed Graphs.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
CMSC 341 Graphs. 5/5/20062 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge.
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.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
CS202 - Fundamental Structures of Computer Science II
CS212: Data Structures and Algorithms
Matrix Representation of Graph
Graphs.
CS202 - Fundamental Structures of Computer Science II
CMSC 341 Lecture 21 Graphs (Introduction)
More Graph Algorithms.
Graphs Graph transversals.
CMSC 341 Graphs.
Paul Beame in lieu of Richard Anderson
CMSC 341 Graphs 3.
CMSC 341 Graphs 3.
Graphs – Adjacency Matrix
Topological Sort Algorithm
Chapter 16 1 – Graphs Graph Categories Strong Components
Graphs G = (V, E) V are the vertices; E are the edges.
Graph Algorithms DS.GR.1 Chapter 9 Overview Representation
Important Problem Types and Fundamental Data Structures
GRAPH – Definitions A graph G = (V, E) consists of
Chapter 9: Graphs Spanning Trees
Presentation transcript:

CMSC 341 Graphs 3

Adjacency Table Implementation Uses table of size |V|  |V| where each entry (i,j) is boolean –TRUE if there is an edge from vertex i to vertex j –FALSE otherwise –store weights when edges are weighted a b e cd a be cd

3 Adjacency Table (cont.) Storage requirement: Performance:

4 Adjacency List Keep list of adjacent vertices for each vertex. –Array of lists of indices: Each element of array[i] is a list of the indices of the vertices adjacent to vertex i. –List of lists: The i-th element of L is associated with vertex v i and is a List L i of the elements of L adjacent to v i. –Lists in Array (NIL sentinels): Each entry a[i,j] is either the index of the j-th vertex adjacent to vertex I or a NIL sentinel indicating end-of-list. –Lists in Array (with valence array): Instead of using NIL sentinels to mark the end of the list in the array, a separate array Valence is kept indicating the number of entries in each row of the array.

5 Adjacency Lists (cont.) Storage requirement: Performance: Array of List ofLists in Lists in Lists Lists Array (NIL) Array (val)

6 Directed Acyclic Graphs A directed acyclic graph is a directed graph with no cycles. A partial order R on a set S is a binary relation such that –for all a  S, aRa is false (irreflexive property) –for all a,b,c  S, if aRb and bRc then aRc is true (transitive property) To represent a partial order with a DAG: –represent each member of S as a vertex –for each pair of vertices (a,b), insert an edge from a to b if and only if aRb.

7 More Definitions Vertex i is a predecessor of vertex j if and only if there is a path from i to j. Vertex i is an immediate predecessor if vertex j if and only if (i,j) is an edge in the graph. Vertex j is a successor of vertex i if and only if there is a path from i to j. Vertex j is an immediate predecessor if vertex i if and only if (i,j) is an edge in the graph.

8 Topological Ordering A topological ordering of the vertices of a DAG G=(V,E) is a linear ordering such that, for vertices i,j  V, if i is a predecessor of j, then i precedes j in the linear order.

9 Topological Sort void TopSort(Graph G) { unsigned int counter = 1 ; Queue q = new Queue(); Vertex indegree[|V|]; for each Vertex v { indegree[v] = getInDegree(v); if (indegree[v] == 0) q.enqueue(v); } while (!q.isEmpty()){ v = q.dequeue(); Put v on the topological ordering; counter++; for each Vertex v adjacent from v { indegree[w] -=1; if (indegree[w]==0) q.enqueue(w); } if (counter <= G.numVertices()) declare an error -- G has a cycle }

10 TopSort Example