# CS112-Problem Set 12 Solved

30.00 \$ 15.00 \$

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . ` zip` solution files instantly, after Payment

## Description

5/5 - (1 vote)

Problem Set 12

Graphs: Topological Sorting, Traversal, Dijkstra’s Algorithm

1. You are given a directed graph:

class Neighbor {Â Â Â Â Â Â  public int vertex;Â Â Â Â Â Â  public Neighbor next;

}

class Vertex {Â Â Â Â Â Â  String name;

}

public class Graph {Â Â Â Â Â Â  Vertex[] vertices;

// returns an array of indegrees of the vertices, i.e. return[i] is the

// number of edges that are directed IN TO vertex iÂ Â Â Â Â Â  public int[] indegrees() {Â Â Â Â Â Â Â Â  // FILL IN THIS METHOD

}Â Â Â Â Â Â  …

}

1. Assuming that the graph has already been read in, complete the indegrees
• What is the big O running time of your indegrees implementation if the graph has n vertices and e edges? Show your analysis.
1. With the same Graph class as in the previous example, assuming that the graph is acyclic, and that that the indegrees method has been implemented, implement a topsort method to toplogically sort the vertices using using BFS (breadthfirst search) (see algorithm in Section 14.4.4 of text):

public class Graph {

public String[] indegrees() {Â Â Â Â Â Â Â Â Â  … // already implemented

}

// returns an array with the names of vertices in topological sequenceÂ Â Â Â Â Â  public String[] topsort() {Â Â Â Â Â Â Â Â  // FILL IN THIS METHOD

}Â Â Â Â Â Â  …Â Â Â  }

You may use the following Queue class:

public class Queue<T> {

public Queue() {…}Â Â Â Â Â Â  public void enqueue(T item) {…}

public T dequeue() throws NoSuchElementException {…}Â Â Â Â Â Â  public boolean isEmpty() {…}

}

1. A strongly connected directed graph is one in which every vertex can reach all other vertices. In the following Graph class, implement a method stronglyConnected that returns true if the graph is strongly connected, and false otherwise. What is the worst case big O running time of your implementation?

public class Graph {Â Â Â Â Â Â  Vertex[] vertices;

// performs a recursive dfs starting at vertex vÂ Â Â Â Â Â  private void dfs(int v, boolean[] visited) {Â Â Â Â Â Â Â Â Â Â  // already implemented

}

public boolean stronglyConnected() {

// FILL IN THIS IMPLEMENTATION

}

}

1. Suppose you are given this undirected graph in which the vertices are towns, and the edges are toll roads between them. The weight of an edge is the dollar amount of toll.

Use Dijsktra’s shortest paths algorithm to determine the minimum toll route from A to all other cities.

Show each step of the algorithm in tabular form. Here’s the table after the initial step:

DoneÂ Â  D[B]Â Â Â  D[C]Â Â Â  D[D]Â Â Â  D[E]Â Â Â  D[F]Â Â Â  D[G]Â Â Â  D[H]Â Â Â  D[I]

———————————————————————Â Â Â Â Â  AÂ Â Â Â  4,AÂ Â Â Â Â  âˆžÂ Â Â Â Â Â  âˆžÂ Â Â Â Â Â  âˆžÂ Â Â Â Â Â  âˆžÂ Â Â Â Â Â  âˆžÂ Â Â Â  8,AÂ Â Â Â Â Â  âˆž

Note that along with the distance, the “previous” vertex is also shown.

Draw the shortest path tree induced on the graph. The shortest path tree is a general tree (a node can have any number of children) that consists of all the vertices of the graph, and the edges that are in the shortest paths to all the vertices. The root of the tree is the source vertex for the algorithm.

• Problem-Set-12-uxg7yg.zip