C S557B JNTU Algorithms Questionnaire

User Generated

NWznah

Computer Science

C S557B

Jawaharlal Nehru Technological University

C

Description

Unformatted Attachment Preview

CS557B: Algorithms Homework-5 Spring 2022 ____________________________________________________________________________________ CS557B Homework-5 Student’s Name: _____________________________________ All work must be your own. Turn in an electronic format (PDF or MS Word file) and any supporting programs (source code and test cases). Show your work in detail. NOTE: No extensions or late submissions for anything other than major emergency. Q1 [15 pts.]: [The depth-first search tree] Solve Problem 7.4a from the Baase’s textbook (page 376). Find the depth-first search tree for the graph used in Example 7.6 (see Figure 7.28, below) with G as the starting vertex under the assumption about the adjacency-list order: a. Each adjacency list is in alphabetical order Q2 [15 pts.]: [The breadth-first search tree] Solve Problem 7.5b from the Baase’s textbook (page 376). Find the breath-first search tree and breath-first distances for the graph used in Example 7.7 (see Figure 7.28, above) with G as the starting vertex under the assumption about the adjacency-list order: b. Each adjacency list is in reverse alphabetical order. Q3 [25 pts.]: [Prim’s minimum spanning tree algorithm] Solve Problem 8.6b from the Baase’s textbook (page 416). 1 CS557B: Algorithms Homework-5 Spring 2022 ____________________________________________________________________________________ Execute Prim’s minimum spanning tree algorithm by hand on the graph in Figure 8.4(a) [see above], showing how the data structures evolve. Clearly indicate which edges become part of the minimum spinning tree and in what order. b. Start at vertex H. Q4 [25 pts.]: [Dijkstra’s shortest-path algorithm] Solve Problem 8.15c from the Baase’s textbook (page 419). Here is the adjacency list (with edge weights in parentheses) for the digraph shown in Figure 8.13 [see above]: A: B(4.0), F(2.0) B: A(1.0), C(3.0), D(4.0) C: A(6.0), B(3.0), D(7.0) D: A(6.0), E(2.0) E: D(5.0) F: D(2.0), E(3.0) c. Execute Dijkstra’s shortest-path algorithm by hand on this graph, showing how the data structures evolve, with s = A. Clearly indicate which edges become part of the shortestpath tree and in what order. Q5 [20 pts.]: Show the Huffman tree and Encoding Table that results from the following distribution (frequency) of punctuation characters and digits: colon (95), space (313), newline (70), comma (35), 1 (221), 2 (164), 3 (57), 5 (241), 6 (153). EXTRA-1: [Kruskal’s minimum spanning tree algorithm] Solve Problem 8.24 from the Baase’s textbook (page 420). 2 CS557B: Algorithms Homework-5 Spring 2022 ____________________________________________________________________________________ Find the minimum spanning-tree for the graph in Figure 8.14 [see above] that would be output by Kruskal’s minimum spanning tree algorithm (Algorithm 8.3), assuming the edges are sorted as shown. EXTRA-2: Implement (create a code) the Prim’s minimum spanning tree algorithm described (as Algorithm 8.1) in the Baase’s textbook (pages 388-399, 422). The program should complete the algorithm, building a data structure to record the minimum spanning tree found. Output should be from a separate procedure and should include the graph, the set of edges in the tree, along with their weights, and the total weight of the tree. 3
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

View attached explanation and answer. Let me know if you have any questions.

CS557B Homework-5 SOLUTION

SOLUTION 1:

Below is the given graph for DFS

DFS for above will be - G D A B C F E
Below is the explanation

Steps:

NOTE: At every step once we visit the node, we will mark it as VISITED so as to avoid re
visiting same node more than once.
1 – Start at G (given in the question)
2 – Mark the node as visited and print it and visit one of the nodes which it is connected to
“G”.
So, first “D” will be visited instead of “E” because the adj list is in alphabetical order

DFS order – G ->D

3 – D has two options “A” and “C” but Visit the node “A” which is alphabetically lesser than
“C”.

DFS order – G ->D -> A
4 – A has 3 options (B, C and F) but visit the node “B” which is adjacent to “A” and
alphabetically least.
5 – Now from B we have 2 options (C, D) but visit the node “C” which is alphabetically
lower.

DFS order – G ->D -> A -> B->C
6 – Now C has not option to go so we will backtrack to “B” and visit the node “D” but since
it is already visited, we will backtrack to “A”
7 –Now in “A” we have visited “B” and visited “C”. Left is “F”. Visit “F” and mark it as
visited.

DFS order – G ->D -> A -> B->C -> F
8 – Now since A has all nodes adjacent to it visited, we will backtrack to “D”.
We see that D also has all adjacent nodes visited. So, we backtrack to “G”.

9 – “G” will now traverse to “E” and mark it as visited.

DFS order – G ->D -> A -> B->C -> F-> E
Now since all nodes visited. DFS is done

Pseudo code for DFS:
public void dfs(ArrayList adj, boolean visited[], int node)
{
visited[node] = true;
System.out.print(node + " ");
while...


Anonymous
Great study resource, helped me a lot.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags