DepthFirstSearch operation can be implemented by using recession. Write the algorithm for a recursive depth first search. Then test the algorithm to verify the results are valid in a driver program
These are the values and weights for the graph
Current After Weight
A B 80
B E 153
E F. 67
F G 67
E D 98
F D 89
D C 78
C F 74
B B 79
Since you have not mentioned the language of your preference, I am providing the code in Java.
CODE
import java.util.*; class Edge implements Comparable<Edge> { private char src, dest; private int weight; public Edge(char src, char dest, int weight) { this.src = src; this.dest = dest; this.weight = weight; } public char getSrc() { return src; } public void setSrc(char src) { this.src = src; } public char getDest() { return dest; } public void setDest(char dest) { this.dest = dest; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } @Override public String toString() { return "Edge{" + "src=" + src + ", dest=" + dest + ", weight=" + weight + '}'; } @Override public int compareTo(Edge o) { if (this.weight > o.getWeight()) { return 1; } else if (this.weight < o.getWeight()) { return -1; } else { return 0; } } } class Graph { private int V; private LinkedHashMap<Character, LinkedList<Edge>> adj; Graph(int v) { V = v; adj = new LinkedHashMap<>(); } void addEdge(char src, char dest, int weight) { if (!adj.containsKey(src)) { adj.put(src, new LinkedList<>()); } adj.get(src).add(new Edge(src, dest, weight)); } void sortMap() { for (Map.Entry<Character, LinkedList<Edge>> entry : adj.entrySet()) { Collections.sort(entry.getValue()); } } void DFSUtil(char v, Map<Character, Boolean> visited) { visited.put(v, true); System.out.print(v + " "); if (adj.get(v) != null) { Iterator<Edge> i = adj.get(v).iterator(); while (i.hasNext()) { Edge n = i.next(); if (!visited.containsKey(n.getDest())) DFSUtil(n.getDest(), visited); } } } void DFS(char v) { Map<Character, Boolean> visited = new HashMap<>(); DFSUtil(v, visited); } } public class Main { public static void main(String[] args) { Graph g = new Graph(7); g.addEdge('A', 'B', 80); g.addEdge('B', 'E', 153); g.addEdge('E', 'F', 67); g.addEdge('F', 'G', 67); g.addEdge('E', 'D', 98); g.addEdge('F', 'D', 89); g.addEdge('D', 'C', 78); g.addEdge('C', 'F', 74); g.addEdge('B', 'B', 79); System.out.println("Following is Depth First Traversal (starting from vertex A): "); g.DFS('A'); } }
OUTPUT
Following is Depth First Traversal (starting from vertex
A):
A B E F G D C
Process finished with exit code 0
DepthFirstSearch operation can be implemented by using recession. Write the algorithm for a recursive depth first...
Help !! I need help with Depth-First Search using an
undirected graph.
Write a program, IN JAVA, to implement the
depth-first search algorithm using the pseudocode given.
Write a driver program, which reads input file mediumG.txt as an
undirected graph and runs
the depth-first search algorithm to find paths to all the other
vertices considering 0 as the
source. This driver program should display the paths in the
following manner:
0 to ‘v’: list of all the vertices traversed to...
Please write in MATLAB code: 1. Write a function called myMin4 that will take in four numbers and returns the minimum value. You may NOT use the built-in min() function. Run the function for the following: myMin4(4, 8, 12, 15) myMin4(18, 9, 1, 6) myMin4(8, -2, 2, 10) 2. Write a function called classAverage that takes in an array of numbers and returns the letter grade of the class average. The grade ranges are as follow: Average >90 = A...
Please
answer all questions! thanks :)
VI/ Test scores from a math midterm are as follows: 79, 90, 85, 89, 70, 59, 75, 64, 83, 78, 75, 77, 78, 77, 67, 85, 74, 52, 87, 72, 69, 76, 61, 77, 93, 86, 79, 90, 74, 67, 51, 75, 77, 82, 78, 60, 86, 72, 91, 95, 82 Complete the frequency distribution table to include all data a. Class Tallies Class Midpoint Relative Cumulative Frequency relative freq boundaries Frequency 51 57...
Using Dev C++, write a program to solve the problem as stated below. Write a program will input a single integer value (all input will be exactly 8 digits long) and will store pairs of digits as individual integer values. The program will display the five 2-digit numbers as well as the average of these, each on their own line. Use a switch statement to print out Grade: and then either A (>=90), B ([80..89]), C ([70..79]), D ([60..69]) or...
Written in Java Your job is to produce a program that sorts a list of numbers in ascending order. Your program will need to read-in, from a file, a list of integers – at which point you should allow the user an option to choose to sort the numbers in ascending order via one of the three Sorting algorithms that we have explored. Your program should use the concept of Polymorphism to provide this sorting feature. As output, you will...
Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from a file that contain positive integers and should insert those numbers into the RB tree in that order. Note that the input file will only contain distinct integers. Print your tree by level using positive values for Black color and negative values for Red color Do not print out null nodes. Format for a node: <Node_value>, <Parent_value>). For example, the following tree is represented...
Using Dev C++, write a program to solve the problem as stated below. Make use of a repetition structure (counter-controlled while loop) to shorten part of your code Dont use array. Write a program will input a single integer value (all input will be exactly 8 digits long) and will store pairs of digits as individual integer values. The program will display the five 2-digit numbers as well as the average of these, each on their own line. Use a...
10. Consider the Traveling Salesperson problem (a) Write the brute-force algorithm for this proble that considers (b) Implement the algorithm and use it to solve instances of size 6, 7, (c) Compare the performance of this algorithm to that of Algorithm all possible tours 8, 9, 10, 15, and 20 6.3 using the instances developed in (b) Algorithm 6.3 The Best-First Search with Branch-and-Bound Pruning Algorithm for the Traveling Salesperson problem Problem: Determine an optimal tour in a weighted, directed...
Using java can someone help me create a program using the
following instructions.
Input file:
10
A J 18
A H 79
A I 81
J G 24
J F 76
G F 2
G H 50
F E 4
E D 2
D H 20
H C 50
I D 6
I C 97
I J 27
C B 22
Solution File:
142
A J G F E D H C B
4. (70 points) Implement Dijkstra's algorithm for...
Write a program to calculate students’ average test scores and their grades. You may assume the following data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three...