I have problem designing pseudo-code for these problems.
It is not an assignment problem.
All I know about them is they have something to do with GRAPH data structure.
-
Describe an
O(n+m)-timealgorithm for computing all the connected components of an undirected graphGwith n vertices and m edges.(I am guessing this has something to do with traversal Breadth First Search (BFS), but correct me if I am wrong.).
Input Graph G Output sequence of connected vertices with edges List = empty list for all u in G.vertices setLabel(u, UNEXPLORED) for all e in G.edges setLabel(e, UNEXPLORED) For all v in G.vertices if getLabel(v) = UNEXPLORED BFS (G,v,List) BFS(G,s,List) Object A = vertex1, vertex2, edge L0 = new empty sequence L0.addLast(s) setLabel(s,VISITED) i=0 while Li is not Empty L(i+1) = new empty sequence for all v in L(i).elements for all incidentEdges(v) if getLabel(e) = UNEXPLORED w = opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e,DISCOVERY) setLabel(w,VISITED) setVertex1(A,v) setVertex2(A,w) setEdge(A,e) List.addLast(A) L(i+1).addLast(w) else setLabel(e,CROSS) i = i + 1 -
Say that an n-vertex directed acyclic graph G is compact.
If there is some way of numbering the vertices of G with integers from 0 to n-1 such that G contains the edge (i,j) if and only if i < j, for all (i , j) in [0,n-1], Give an O(n^2)-time algorithm for detecting if G is compact.(Again, I am guessing this has something to do with topological ordering, but I am not sure how to implement it).
-
Say a connected graph G is biconnected if it contains no vertex whose removal would divide G into 2 or more connected components.
Give an O(n+m)-time algorithm for adding at most n edges to a connected graph G, with n>= 3 vertices and m>=(n-1) edges, to guarantee that G is biconnected. (Probably spanning forest?).
I had fun with these problems! At least, 2 and 3.
1)
I’m not sure I fully understand, but I figure by “compute the connected components” you mean “build subsets of the vertices such that each subset is a connected component”. If so, I think BFS or DFS would work depending on how you manage memory (ie. how you mark vertices you’ve already visited).
2)
[Edited]Here’s an algorithm which, used on any acyclic directed graph, should number the vertices according to the “compact” definition and detect if the graph is in fact compact (ie. contains all edges (i, j) such that i < j for all (i, j) in [0, n-1]).
1a. If there are more than one, terminate algorithm: the graph is not compact.
(This is also O(n^2), btw – n searches over n vertices to find vertices without incoming edges – although this is only worst-case)
At the end of this, all vertices will be numbered such that if it has incoming edges, the nodes from which they come will have a lower number than itself.
3)
Assuming the graph is already connected, here is an algorithm to make it biconnected:
(I think this actually fits O(n). One search over n vertices to find endpoints, and we add less than n edges, since you can’t have a connected graph consisting entirely of endpoints.)
Voila! A biconnected graph! Remove any endpoint vertex, and the original connected graph is still intact; remove any other vertex, and we know that each segment is still connected through the endpoints.