Suppose you are given a directed graph G (using an adjacency list) and a vertex
ID: 3727540 • Letter: S
Question
Suppose you are given a directed graph G (using an adjacency list) and a vertex v. For the following, write pseudocode and show the worst-case running time (a) Implement an algorithm that deletes v from G and all edges connected to v. (b) For all adjacent vertices of v, delete an adjacent vertex u if and only if u contains a single directed edge to v. Then, it should check the adjacent vertices of u to see if this deletion property still holds true. And then, check all adjacent vertices of the adjacent vertices of u. And so on and so forth. This deletion property should cascade throughout G as long as it holds true for any adjacent vertex. Hint: It might help to use the algorithm you designed in part (a)Explanation / Answer
(A)
Since a directed graph is provided, first the vertex v can be deleted from the adjacency list. Then, perform a linear search over all vertices and search for the vertex v, if it is present delete it, otherwise, do nothing. Deleting the vertex takes O(1) time while searching for the edge along all vertices takes O(V+E) time.
Total time complexity O(V+E)
PSEUDOCODE:
Funtion (Remove(x))
Delete(x) //where x is the vertex to be deleted
for i in v:
for j in v[i]:
if x found:
Delete(x)
(B)
PSEUDOCODE
Function (RemoveAll())
for i in v;
for j in v[i]:
if j belongs to v[i] and i does not belong to v[j[:
Remove(j)
Note that Remove function done in part A deletes the vertex along the entire graph and checks for the same.
Time Complexity = O(V+E)*O(V+E) = O(V+E)2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.