CMSC 341 Graphs 3.

Slides:



Advertisements
Similar presentations
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Advertisements

Chapter 9: Graphs Topological Sort
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.
Topological Sort and Hashing
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Data Structures Using C++
1 Graph Theory © Dave Bockus. 2 Adjacency Matrix
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 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.
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.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Python: Advanced Objects
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.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
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.
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.
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.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
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.
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.
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
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.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
CMSC 341 Graphs. 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w)
CS212: Data Structures and Algorithms
Matrix Representation of Graphs
CMSC 341 Graphs.
Depth-First Search.
SINGLE-SOURCE SHORTEST PATHS IN DAGs
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
Topological Sort CSE 373 Data Structures Lecture 19.
Graph Operations And Representation
CMSC 341 Graphs 3.
CMSC 341 Lecture 20.
Connected Components, Directed Graphs, Topological Sort
Graphs – Adjacency Matrix
ITEC 2620M Introduction to Data Structures
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
Heaps Chapter 6 Section 6.9.
Introduction to Graphs
Chapter 9: Graphs Spanning Trees
CMSC 341 Graphs.
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 a b e b e c d c d

Adjacency Table (cont.) Storage requirement: Performance: Storage: O(|V|^2) getDegree(u) O(|V|) -- must look at (p-1) elements on the u-th row (u-th column, u-th row getAdjacent(u) O(|V|) -- must look down the u-th row getAdjacentFrom(u) O(|V|) -- must look across the u-th column isAdjacent(u,v) O(1) -- random access to element[u,v]

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 vi and is a List Li of the elements of L adjacent to vi. 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.

Adjacency Lists (cont.) Storage requirement: Performance: Array of List of Lists in Lists in Lists Lists Array (NIL) Array (val) Storage: O(|V| + |E|) getDegree(u) -- O(D(u)) -- requires counting list length in each case except “Lists in Array (with valence)” which is O(1) getInDegree(u) -- O(|V|+|E|) -- requires searching each list for the presence of vertex u getOutDegree(u) -- O(D(u)) -- requires counting list length in each case except getAdjacent(u) -- O(D(u)) -- requires construction of a list of each vertex of each vertex on the adjacency list of vertex u getAdjacentFrom(u) -- O(|V|+|E|) for “Array of lists” and “List of Lists” O(|V|^2) for “Lists in Array” requires construction of a list of vertices by searching the adjacency lists of each vertex isAdjacent(u) - O(D(u)) -- requires searching the adjacency list of vertex u for the presence of vertex v Storage -- O(|V|+|E|) for “Array of Lists” and “List of Lists” -- entry for each vertex and list of entries for each adjacent vertex O(|V|^2) for “Lists in Array” -- entry for each vertex

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 Example: DAG for CS courses

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.

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. In general, there is more than one topological ordering for a graph. Ex: legitimate orders to take CS courses

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 Q: In what circumstances can counter be equal to NumVerts? A: One circumstance would be a graph with a single vertex and a directed edge from the vertex to itself.

TopSort Example 1 2 3 4 5 6 7 8 9 10 Indegree array at start: 1 2 3 4 5 6 7 8 9 10 Indegree array at start: 1 2 3 4 5 6 7 8 9 10 1 2 2 0 2 1 0 3 2 0 Step Queue v counter 0 1 1 4,7,10 1 2 7,10 4 2 3 10 7 3 4 1,3 10 4 5 3,5 1 5 6 5,6,2 3 6 7 6,2,9 5 7 8 2,9 6 8 9 9 2 9 10 8 9 10