Answers to Assignment #2

Slides:



Advertisements
Similar presentations
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.
Advertisements

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Topological Sort and Hashing
Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs – Depth First Search ORD DFW SFO LAX
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Implementation of Graph Decomposition and Recursive Closures Graph Decomposition and Recursive Closures was published in 2003 by Professor Chen. The project.
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.
Shortest Path Problems
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
1 Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithms. An Example. Implementation.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
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.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
CS112A1 Spring 2008 Practice Final. ASYMPTOTIC NOTATION: a)Show that log(n) and ln(n) are the same in terms of Big-Theta notation b)Show that log(n+1)
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
Graphs.
© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Topological Sort: Definition
Graph Connectivity This discussion concerns connected components of a graph. Previously, we discussed depth-first search (DFS) as a means of determining.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 7: Sorting and Directed Graphs.
 2004 SDU 1 Lecture5-Strongly Connected Components.
Graphs and Shortest Paths Using ADTs and generic programming.
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);
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Week 11 - Wednesday.  What did we talk about last time?  Exam 2  And before that:  Graph representations  Depth first search.
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.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Topological Sort. Sorting technique over DAGs (Directed Acyclic Graphs) It creates a linear sequence (ordering) for the nodes such that: –If u has an.
Introduction to Algorithms
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Topological Sort (an application of DFS)
Depth-First Search.
Topological Sort.
Topological Sort.
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
BFS,DFS Topological Sort
Advanced Algorithms Analysis and Design
"Learning how to learn is life's most important skill. " - Tony Buzan
Topological Sort CSE 373 Data Structures Lecture 19.
Topological Sort (an application of DFS)
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Assignment #2 (Assignment due: Nov. 06, 2018) v1 v2 v3 v4 v5
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Answers to Assignment #2 1.(30) See the graph G shown in Fig. 1(a), which can be stored in a file on disk as a set of pairs as shown in Fig. 1(b). Design an algorithm to load the file in main memory and store it as a set of linked lists. (The algorithm should be able to be used for any graph.) (v1, v2) (v1, v3) (v1, v4) (v1, v5) (v2, v3) (v2, v4) (v2, v5) (v3, v4) (v3, v5) (v4, v5) v1 v2 v3 v4 v5 (a) (b) Fig. 1 Each pair represents an edge.

Answer: Assume that the graph contains 1000 nodes numbered 1, 2, 3, …, 999. Define a new type Edge with two data fields: class Edge {int id; Edge next) Algorithm graphStorage Edge[] nodes = new Edge[1000];//initially, each node[i] is null. Edge[] points = new Edge[1000];// initially, each node[i] is null. for each pair (x, y) in the file do { z := new Edge(y, null); if points[x] == null then {nodes[x] := z;} else {points[x].next := z; points[x] := z; } x points … x nodes … … y

2.(30) Another way to perform topological sorting on a directed acyclic graph G = (V, E) is to repeatedly find a node of in-degree 0, output it, and remove it and all of its outgoing edges from the graph. Explain how to implement this idea so that it runs in time O(|V| + |E|). Answer: Algorithm TopologicalOrder(G) Begin let v1, ..., vk be all the nodes with indegree = 0; for i = 1 to k do {enqueue(Q, vi);} while (Q is not empty) do { v := dequeue(Q); output(v); for (any node u with v  u  E ) do { indegree(u) := indegree(u) - 1; if indegree(u)= 0 then enqueue(w; }

Assignment #2 v1 v1 v2 v2 v3 v4 v3 v5 v4 v5 3.(40) Consider the graph G shown in Fig. 1(a) once again. Its DFS tree T is shown in Fig. 2(a), in which each node v is associated with (d[v], f[v]). Remember that a node is a descendant of another node v in T if d[v] < d[u]) < f[u] < f[v]). v1 (1, 10) v1 (1, 10) v2 (2, 9) (2, 9) v2 v3 v4 v3 (a) (3, 6)(7, 8) v5 (b) (7, 8) (3, 6) v4 v5 (4, 5) (3, 6) (4, 5) Fig. 2 Describe an algorithm that associates each node v in G with a sequence s of pairs (as shown in Fig. 2(b)) such that u is a descendant of v in G if and only if d[v] < d[u]) < f[u] < f[v]), or there exists a pair p = (a, b) in s with a  d[u]) < f[u]  b.

The working process mainly consists of two steps. In the first step, we will generate the time stamps for all nodes in G. In the second step, we will generate the pair sequence for each node. In the following algorithm, we will use v.d and v.f to refer to the discovery time stamp and the finishing time stamp of v, respectively. We will also use v.L to refer the pair sequence created for v. Algorithm SequenceGeneration (G) Begin Create the time stamp (d[v], f[v]) for each node v by using DFS; find a topological sequence of G: v1, v2, …, vn; For j = n to 1 do { let u1, …, uk be the children of vj; For m = 1 to k do { vj.L := merge(vj.L, um.L); } } end

Function merge(L1, L2) Begin i : = 1; j := 1; while (i ≤ |L1| and j ≤ |L2|) do { if L1(i).f < L2(j).d then i := i + 1; If L1(i)  L2(j) then j := j + 1; If L2(j).f < L1(i).d then { insert L2(i) into L1 just before L1(j); j := j + 1; } If L2(j)  L1(i) then {remove L1(i) from L1; i := i + 1;} } if j ≤ |L2| then put the remaining pairs in L2 at the end of L1; return L1; end Example L1 = [1, 3][6, 9][11, 15] and L2 = [4, 5][7, 8][16, 16]. merge(L1, L2) = [1, 3][4, 5][6, 9][11, 15][16, 16].