Question

In: Computer Science

We can use breadth-first search to find the length of longest simple path in the BFS...

We can use breadth-first search to find the length of longest simple path in the BFS tree
starting at s by the simple method of checking each v.d value at the end of the algorithm. BFS is
Θ(|V | + |E| and this adds only Θ(|V |) work.
Find an even easier approach that adds only constant time (Θ(1)) work via a simple modification to
the BFS algorithm.

Solutions

Expert Solution

If a graph G is given, then we mostly use the current vertex and G to do BFS.

If we use a count parameter as well doing bfs then we can find the longest path in Θ(1) work.

BFS (G, s)                   //Where G is the graph and s is the source vertex
      let Q be queue.
      Q.enqueue( s, 1 ) //Inserting s in queue and the size of path till s
      mark s as visited.
      while ( Q is not empty)
           //Removing that vertex from queue
           v  =  Q.dequeue( )
           maxpath= max(maxpath,v.dist)     // v.dist gives the length of path till this vertex
          //processing all the neighbours of v  
          for all neighbours n of v in Graph G
               if w is not visited 
                        Q.enqueue( w , v.dist+1)    //Stores w in Q to further visit its neighbour
                        mark w as visited.

maxpath stores the length of the maximum path of the graph i.e. our solution.

Rather than maintaining queue of vertices in BFS, we maintain queue of pair of vertice and the length of path till it.


Related Solutions

create a code in JAVA that takes arguments to do a bfs(breadth first search) or a...
create a code in JAVA that takes arguments to do a bfs(breadth first search) or a dfs( depth first search) using a txt file as the graph input. I have made most of the main function as well as a nose function I just need the bfs and dfs functions to work here's what I have so far, the comments that start with TODO are the parts that I need to finish Main.java import java.io.*; import java.util; ArrayList; import java.util.Iterator;...
In this assignment, you will implement Breadth First Search (BFS). The input to your program is...
In this assignment, you will implement Breadth First Search (BFS). The input to your program is a graph in the adjacency list format. The input graph has at most 100 vertices. Your program must initiate a BFS from vertex 2 of the graph. If the graph is connected, your program must output “Graph is connected”. If the graph is disconnected, your program must output “Graph is not connected”. Your program should read from an input file: data2.txt and write to...
3.1 Bratko states that iterative deepening combines the best properties of breadth-first search and depth-first search...
3.1 Bratko states that iterative deepening combines the best properties of breadth-first search and depth-first search and is therefore, in practice, often the best choice amongst the basic search methods. Discuss this statement. 3.2 Discuss the concept of the ‘locality’ of the effects of actions in the context of planning problems.
Write a Python application to find the longest ‘A’ path on a map of capital (uppercase)...
Write a Python application to find the longest ‘A’ path on a map of capital (uppercase) letters. The map is represented as a matrix (2-dimensional list) of capital letters. Starting from any point, you can go left, right, up and down (but not diagonally). A path is defined as the unbroken sequence of letters that only covers the spots with the letter A. The length of the A path is defined as the number of ‘A’s on the path. Your...
give an example of a simple, undirected, weighted graph such that breadth-firstsearch outputs a search-tree that...
give an example of a simple, undirected, weighted graph such that breadth-firstsearch outputs a search-tree that is not a single source shortest path tree. Youranswer must (a) Specify the graphG= (V, E)by specifyingVandE. (b) Specify the weight functionw. (c) Specify an ordering of the vertices and the search-tree output by breadth-first search assuming the specified graph and ordering. (d) Specify a valid single-source shortest path treeT= (V, ET)by specifyingETand its root, the first vertex in your specified ordering. (e) Include...
In the shortest-path algorithm we are concerned with the total length of the path between a...
In the shortest-path algorithm we are concerned with the total length of the path between a source and every other node. Suppose instead that we are concerned with the length of the longest edge between the source and every node. That is, the bottleneck of a path is defined to be the length of the longest edge in the path. Design an efficient algorithm to solve the single source smallest bottleneck problem; i.e. find the paths from a source to...
Give a recurrence relation to find the length of the longest monotonically increasing subsequent of a...
Give a recurrence relation to find the length of the longest monotonically increasing subsequent of a array. Give the recursive algorithm based on the recurrence relation and explain the time complexity of this algorithm.
We use binary search tree because in best case scenario we can retrieve anything we search...
We use binary search tree because in best case scenario we can retrieve anything we search for in O(log(n)) times. However, this is not always the case. Give an example of when this fails and what can be done to avoid it.
1. What is the role of “levels” within a breadth-first search? 2. Other than flight routes,...
1. What is the role of “levels” within a breadth-first search? 2. Other than flight routes, what is an example of directed graphs in the real world? 3. Other than driving routes, what is an example of shortest paths in the real world? 4. What is the difference between shortest path and minimum spanning trees? 5. What role does “NP” play when developing efficient algorithms?
Use the Bohr model to find the longest wavelength of light in the Paschen
Use the Bohr model to find the longest wavelength of light in the Paschen
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT