Given an unweighted directed graph G, write a program that counts and prints all simple paths from a given ‘s’ to a given ‘d’. Assume the graph G is represented using adjacent matrix.
Using Java Language
// JAVA program to print all paths from a s to d
import java.util.ArrayList;
import java.util.List;
public class Graph {
private int v; // No.
of vertices in graph
private ArrayList<Integer>[]
adjList; // adjacency list
public Graph(int vertices) {
this.v = vertices;
initAdjList();
}
private void initAdjList() {
adjList = new ArrayList[v];
for(int i = 0; i < v; i++)
{
adjList[i] = new
ArrayList<>();
}
}
public void addEdge(int u, int v) {
adjList[u].add(v);
}
public void printAllPaths(int s, int d) {
boolean[] isVisited = new
boolean[v];
ArrayList<Integer> pathList =
new ArrayList<>();
pathList.add(s);
printAllPathsUtil(s, d, isVisited,
pathList);
}
private void printAllPathsUtil(Integer u, Integer
d, boolean[] isVisited, List<Integer> localPathList) {
isVisited[u] = true;
// Mark the current node
if (u.equals(d)) {
System.out.println(localPathList);
isVisited[u]=
false; // if match found then no
need to traverse more till depth
return ;
}
// Recur for all the vertices
adjacent to current vertex
for (Integer i : adjList[u])
{
if
(!isVisited[i]) {
// store current node in path[]
localPathList.add(i);
printAllPathsUtil(i, d, isVisited,
localPathList);
// remove current node in path[]
localPathList.remove(i);
}
}
isVisited[u] = false;
// Mark the current node
}
public static void main(String[] args) {
Graph g = new Graph(4);
g.addEdge(0,1);
g.addEdge(0,2);
g.addEdge(0,3);
g.addEdge(2,0);
g.addEdge(2,1);
g.addEdge(1,3);
int s = 2;
// arbitrary source
int d = 3;
// arbitrary destination
System.out.println("Following are
all different paths from "+s+" to "+d);
g.printAllPaths(s, d);
}
}
Given an unweighted directed graph G, write a program that counts and prints all simple paths...
You are given an unweighted DAG (Directed Acyclic Graph) G, along with a start node s and a target node t. Design a linear time (i.e., runtime O(IV+ EI)) dynamic programming algorithm for computing the number of all paths (not necessarily shortest) from s to t.
IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smart) update to the add function we discussed in class. a) 25 points Create the following binary trees by creating the nodes (malloc) and setting the related pointers (leftChild, right Child). Make content random. 50 40 60 100 70 45 30 120 47 b) 25 points Display the trees on screen the way we did in slide 9...
Write a program that specifies a simple undirected graph by its “adjacency matrix”. Recall that that the adjacency matrix A is such that A(i, j) = 1 if nodes i and j are adjacent and A(i, j) = 0 otherwise. Let αk(i ,j) be the number of paths of length k between nodes i and j. For instance, the number of paths of length-1 between nodes i and j in a simple undirected graph is 1 if they are adjacent...
5. Suppose we are given an unweighted, directed graph G with n vertices (labelled 1 to n), and let M be the n × n adjacency matrix for G (that is, M (i,j-1 if directed edge (1J) is in G and 0 otherwise). a. Let the product of M with itself (M2) be defined, for 1 S i,jS n, as follows where "." is the Boolean and operator and "+" is the Boolean or operator. Given this definition what does...
Using Java language, Write a program that read a string from the keyboard and counts the number of the letter A and the number of letter B and then prints the result
This question needs to be done using pseudocode (not any particular programming language). Thanks Consider an unweighted, undirected graph G = 〈V, E). The neighbourhood of a node u E V in the graph is the set of all nodes that are adjacent (or directly connected) to v. Subsequently, we can define the neighbourhood degree of the node v as the sum of the degrees of all its neighbours (those nodes that are directly connects to v) (a) Design an...
Consider an unweighted, undirected graph G = 〈V, E). The neighbourhood of a node u E V in the graph is the set of all nodes that are adjacent (or directly connected) to v. Subsequently, we can define the neighbourhood degree of the node v as the sum of the degrees of all its neighbours (those nodes that are directly connects to v) (a) Design an algorithm that returns a list containing the neighbourhood degree for each node v V,...
Write a program that discovers and displays all the Hamiltonian Cycles of a Weighted, Non-directed graph (In Java).
For the directed weighted graph given below find shortest distances and shortest paths from A to all other vertices. Use the Dijkstra algorithm. Show the status of the array of distances after each iteration of the while loop. 2-1 C ) 泊 H e- 90油 2 2 22 (4-21由121回 G
Exercise 2 Given the following graph: a. Write the formal description of the graph, G=(V,E) b. Show the Adjacency Matrix representation C. Show the Adjacency List representation d. Calculate step by step the shortest paths from a e. Show the DFS tree/forest from a f. Show the BFS tree/forest from a g MST using Prim h. MST using Kruskal