CMSC 341 Graphs 3.

Slides:



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

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
Minimum cost spanning tree and activity networks Data structure 2002/12/2.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
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 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.
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.
CSE 326: Data Structures Lecture #17 Trees and DAGs and Graphs, Oh MY! Bart Niswonger Summer Quarter 2001.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
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.
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.
Properties of Relations In many applications to computer science and applied mathematics, we deal with relations on a set A rather than relations from.
Chapter 9. Chapter Summary Relations and Their Properties n-ary Relations and Their Applications (not currently included in overheads) Representing Relations.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
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.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter 9. Chapter Summary Relations and Their Properties n-ary Relations and Their Applications (not currently included in overheads) Representing Relations.
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 Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
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.
SSSP in DAGs (directed acyclic graphs). DFS (depth first search) DFS(vertex v) { v.visited = TRUE; for each w adjacent to v do if(!w.visited) then dfs(w);
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
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
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
"Learning how to learn is life's most important skill. " - Tony Buzan
Search Related Algorithms
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
Topological Sort Algorithm
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Graphs Chapter 7 Visit for more Learning Resources.
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
GRAPH – Definitions A graph G = (V, E) consists of
Heaps Chapter 6 Section 6.9.
CMSC 341 Graphs.
Presentation transcript:

CMSC 341 Graphs 3

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 theend 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 isConnected(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 topo 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.

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 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