Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 341 Graphs 3.

Similar presentations


Presentation on theme: "CMSC 341 Graphs 3."— Presentation transcript:

1 CMSC 341 Graphs 3

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

3 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

4 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

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

6 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

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

8 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: Step Queue v counter 0 1 1 4,7,10 1 2 7,10 4 2 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


Download ppt "CMSC 341 Graphs 3."

Similar presentations


Ads by Google