Docsity

Algorithm Class Tutorial: Breadth & Depth First Traversal, Graph Bipartiteness Solutions, Exercises of Algorithms and Programming

Partial preview of the text

Download Algorithm Class Tutorial: Breadth & Depth First Traversal, Graph Bipartiteness Solutions and more Exercises Algorithms and Programming in PDF only on Docsity!

Algorithms Tutorial questions 2019 Semester 1 Tutorial 3

The University of Sydney School of Computer Science

Problem 1

Run a Breadth First Traversal for graph G starting at A. Write the order the nodes are explored.

A

B

C

D

E F

G

H

I

Solution: Assuming breaking ties lexicographically (i.e., if more than one child pick the one that comes first in lexicographic order). A B,E C,D,F,H G, I

Problem 2

Run a Depth First Traversal for graph G starting at A. Write the order the nodes are explored.

A

B

C

D

E F

G

H

I

Solution: Assuming breaking ties lexicographically (i.e., if more than one child pick the one that comes first in lexicographic order). A B C D E F G H I

Problem 3

Run a Breadth First Search for graph G starting at A and searching for I. Write out the path that is explored.

A

B

C

D

E F

G

H

I

Solution: A B,E C,F,H D,G,I

Problem 4

Problem 5 Consider the following directed graph G

A

B

C

D

E F

G

H

  1. Give the adjacency matrix and the adjacency list representation of this graph
  2. Is this graph strongly connected?
  3. This graph has a few directed cycles. Find the minimum number of edges that you need to delete from this graph in order to make it a directed acyclic graph (DAG).
  4. Write down a topological ordering of the resulting DAG.

Solution:

  1. adjacency matrix: A B C D E F G H A 0 0 0 0 1 0 0 0 B 1 0 1 0 0 0 0 0 C 0 0 0 1 0 0 0 0 D 0 1 0 0 1 0 1 0 E 0 0 0 1 0 1 0 0 F 0 0 0 0 0 0 0 1 G 0 0 0 0 0 1 0 1 H 0 0 0 0 0 0 0 0 adjacency list: A: E B: A,C C: D D: B,E,G E: D,F F: H G: F,H H:
  2. No, H is a sink. There is no path from H to any other node. Note that you do not need to check all possible ordered pairs of vertices to see if the graph is connected. Just pick one vertex (any will do) and check if that particular vertex has a path to every other vertex and every other vertex has a path back to it.
  3. delete edges (D,B) and (D,E) to get a DAG.
  4. In the resulting graph, B is a source. Add to the sequence as the first node and delete it from the graph along with all edges incident to it. Now A is a source (as well as C, we go with the lexicographically first- but any choice is fine in breaking symmetry/ties), then take C, followed by E, D, G, F, H. So the topological order is B,A,C,D,E,G,F,H. You can try the same by starting with sinks.

Problem 6 An undirected graph G = (V, E) is said to be bipartite if its vertex set V can be partition into two sets A and B such that E โІ A ร— B. Design an O(n + m) algorithm to test if a given input graph is bipartite using the following guide:

  1. Suppose we run BFS from some vertex s โˆˆ V and obtain layers L 1 ,... , Lk. Let (u, v) be some edge in E. Show that if u โˆˆ Li and v โˆˆ Lj then |i โˆ’ j| โ‰ค 1.
  2. Suppose we run BFS on G. Show that if there is an edge (u, v) such that u and v belong to the same layer then the graph is not bipartite
  3. Suppose G is connected and we run BFS. Show that if there are no intra-layer edges then the graph is bipartite
  4. Put together all the above to design an O(n + m) time algorithm for testing bipartiness.