Question

2. Let G be an undirected graph. For every u,vE V(G), let dc(u,v) be the length of the shoertest path from u to v. The diamet

0 0
Add a comment Improve this question Transcribed image text
Answer #1

 
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=1000005;
  4. int n,m,dp[maxn],best,path[maxn],root;
  5. vector<pair<int,int>> g[maxn];
  6. int dfs_With_dp(int u)
  7. {
  8. if(dp[u]) return dp[u];
  9. dp[u]=1;
  10. for(auto v:g[u]) {
  11. int c=v.first;
  12. if(dp[u]<dfs_With_dp(c)+1) {
  13. dp[u]=dp[c]+1;
  14. path[u+1]=c+1;
  15. }
  16. }
  17. return dp[u];
  18. }
  19. int main()
  20. {
  21. int x,y;
  22. scanf("%d%d",&n,&m);
  23. for(int i=0;i<m;i++) {
  24. scanf("%d%d",&x,&y);
  25. x-=1;
  26. y-=1;
  27. g[x].push_back(make_pair(y,i+1));
  28. }
  29. for(int i=0;i<n;i++) {
  30. int path_len=dfs_With_dp(i);
  31. if(best<path_len) {
  32. root=i;
  33. best=path_len;
  34. }
  35. }
  36. cout<<"longest path length : "<<best-1<<endl;
  37. int u=root+1;
  38. cout<<"path Sequence: ";
  39. while(path[u]!=0) {
  40. cout<<u<<" ";
  41. u=path[u];
  42. }
  43. cout<<u<<endl;
  44. return 0;
  45. }

Here the longest path obtained as output is G.

Add a comment
Know the answer?
Add Answer to:
2. Let G be an undirected graph. For every u,vE V(G), let dc(u,v) be the length of the shoertest ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. (15 pts) The diameter of a graph is the largest of all shortest-path distances in the graph. I...

    3. (15 pts) The diameter of a graph is the largest of all shortest-path distances in the graph. In other words, if So(x, y) is the length of the shortest path from| x to y in graph G = (V,E), then the diameter of G is max (Sc(t,y)J Give an algorithm to compute the diameter of an undirected graph G (V. E), with running time at lnost O (V12 + VİİE). Include an analysis of the running time. 3. (15...

  • Reachability. You are given a connected undirected graph G = (V, E ) as an adjacency...

    Reachability. You are given a connected undirected graph G = (V, E ) as an adjacency list. The graph G might not be connected. You want to fill-in a two-dimensional array R[,] so that R[u,v] is 1 if there is a path from vertex u to vertex v. If no such path exists, then R[u,v] is 0. From this two-dimensional array, you can determine whether vertex u is reachable from vertex v in O(1) time for any pair of vertices...

  • 2) Let G ME) be an undirected Graph. A node cover of G is a subset...

    2) Let G ME) be an undirected Graph. A node cover of G is a subset U of the vertex set V such that every edge in E is incident to at least one vertex in U. A minimum node cover MNC) is one with the lowest number of vertices. For example {1,3,5,6is a node cover for the following graph, but 2,3,5} is a min node cover Consider the following Greedy algorithm for this problem: Algorithm NodeCover (V,E) Uempty While...

  • Suppose you are given an undirected graph G. Find a pair of vertices (u, v) in...

    Suppose you are given an undirected graph G. Find a pair of vertices (u, v) in G with the largest number of common adjacent vertices (neighbors). Give pseudocode for this algorithm and show the worst-case running time.

  • 2. Let G = (V, E) be an undirected connected graph with n vertices and with...

    2. Let G = (V, E) be an undirected connected graph with n vertices and with an edge-weight function w : E → Z. An edge (u, v) ∈ E is sparkling if it is contained in some minimum spanning tree (MST) of G. The computational problem is to return the set of all sparkling edges in E. Describe an efficient algorithm for this computational problem. You do not need to use pseudocode. What is the asymptotic time complexity of...

  • 1. Here is a randomized algorithm for MAX-CUT on an undirected graph G = (V,E): 1....

    1. Here is a randomized algorithm for MAX-CUT on an undirected graph G = (V,E): 1. Initialize S to be the empty set 2. For every vertex v in V: 3. Put v into S with probability 1/2 4. Let T = V\S be the complement of S 5. Output (S,T) Recall that E(S, T) CE is the set of edges that have one endpoint in S and the other endpoint in T, ie., E(S, T) = {(u, u) :...

  • Let G = (V, E, w) be a connected weighted undirected graph. Given a vertex s...

    Let G = (V, E, w) be a connected weighted undirected graph. Given a vertex s ∈ V and a shortest path tree Ts with respect to the source s, design a linear time algorithm for checking whether the shortest path tree Ts is correct or not.(C pseudo)

  • Let G be a non-Hamiltonian, connected graph. For every pair of nonadjacent vertices u and v, 8(u)...

    Let G be a non-Hamiltonian, connected graph. For every pair of nonadjacent vertices u and v, 8(u) +8()2 k, for some k> O. Show that G contains a path of length k. Let G be a non-Hamiltonian, connected graph. For every pair of nonadjacent vertices u and v, 8(u) +8()2 k, for some k> O. Show that G contains a path of length k.

  • B-1 Graph coloring Given an undirected graph G (V. E), a k-coloring of G is a function c : V → {0, 1, . . . ,k-1}

    B-1 Graph coloring Given an undirected graph G (V. E), a k-coloring of G is a function c : V → {0, 1, . . . ,k-1} such that c(u)≠c(v) for every edge (u, v) ∈ E. In other words, the numbers 0.1,... k-1 represent the k colors, and adjacent vertices different colors. must havec. Let d be the maximum degree of any vertex in a graph G. Prove that we can color G with d +1 colors.

  • graph G, let Bi(G) max{IS|: SC V(G) and Vu, v E S, d(u, v) 2 i},...

    graph G, let Bi(G) max{IS|: SC V(G) and Vu, v E S, d(u, v) 2 i}, 10. (7 points) Given a where d(u, v) is the length of a shortest path between u and v. (a) (0.5 point) What is B1(G)? (b) (1.5 points) Let Pn be the path with n vertices. What is B;(Pn)? (c) (2 points) Show that if G is an n-vertex 3-regular graph, then B2(G) < . Further- more, find a 3-regular graph H such that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT