The reverse of a directed graph G = (V,E) is another directed graphGR=(V,ER) on the same vertex set, but with all edges reversed that is,ER={(v,u):(u,v)E} . Give a linear-time algorithm for computing the reverse of a graph in adjacency list format.

Short Answer

Expert verified

A linear-time algorithm is used for reversalGR=(V,ER) of a directed graph G=(V,E) by using an adjacency list is proved.

Step by step solution

01

Step 1: Adjacency List

The representation of the list contains an array known as an adjacency list.

An adjacency list consists of a linked list for each vertex.

The size of the array is the same as the number of vertices in it.

If the graph is an undirected graph, then each edge appears twice in the list.

Each list consists of the name of the vertex.

Reversing the adjacency lists of a Directed Graph can be done in linear time

Order of complexity will be O(V+E).

02

Reversal of graph takes linear time

Consider directed graph G =(V,E) which has vertices and edges. Firstly, perform the reversal of graph that is GR=(V,ER). Let’s perform the reverse of the graph by changing the directions of edges that are connected to the vertex. After the reversal of the graph all the edges reversed that is, E={(v,u):(u,v)E}is shown in the figure below.

The degree of a node in a directed graph is the number of edges connected to the node. For each node in a graph there is a list. It will take a linear time in adjacency list and it always assign a degree value to each node. And while iterating from the list the total number of the vertex is the degree of the vertex. After reversal of the graph take the adjacency list and put all the vertices in the adjacency list one by one.

Draw the Reversal of the directed graph:

Adjacency list for directed graph is shown below:

As in this directed graph it contains five vertices so the size of the adjacency list here is five. After that as known that the graph is the directed graph so, the vertex connected by edges to the other vertex writes only those vertices in front of the list that take a temp as a variable for storing the temporary variable while traversing the directed graph to the adjacency list. Like here vertex one is connected by vertex two and vertex two is connected to vertex three like that mark in the adjacency list. Here each vertex has one linked list. After performing this step now, the graph contained the adjacency list. Where the time complexity is O (V+E) . Hence it is proved that this is a linear-time algorithm that takes O (V+E) time.

03

Prove by using example

Now take another example where a graph G=(V,E)which have vertices and edges. Firstly, perform the reversal of graph that is GR=(V,ER). Draw a directed graph for 5 vertices.

Let’s perform reverse of graph by changing the directions of edges that are connected to the vertex. After reversal of graph all the edges reversed that isE={(v,u):(u,v)E},is shown in the figure below. Here five vertices and every edge are bidirectional so here in this example each vertex has a degree of two.

The indegree of each vertex is two and the outdegree of each vertex is also two. Now after performing the reversal of graph then put the vertices into the adjacency list. Where this list contain array named as adjacency list and it consists of a linked list for each vertex.

Here the reversing the adjacency lists of a Directed Graph can be done in linear time. And the order of complexity will be O (V+E).

Hence it is proved that a linear-time algorithm is used for computing the reversal of a graph by using adjacency list.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Question:Undirected vs. directed connectivity.

(a) Prove that in any connected undirected graph G =(V , E)there is a vertexvV whose removal leaves G connected. (Hint: Consider the DFS search tree for G.)

(b) Give an example of a strongly connected directed graph G(V ,E)such that, for everyvV, removing v from G leaves a directed graph that is not strongly connected.

(c) In an undirected graph with two connected components it is always possible to make the graph connected by adding only one edge. Give an example of a directed graph with two strongly connected components 0 such that no addition of one edge can make the graph strongly connected.

On page 102, we defined the binary relation “connected” on the set of vertices of a directedgraph. Show that this is an equivalence relation(see Exercise 3.29), and conclude that it partitions the vertices into disjoint strongly connected components.

As in the previous problem, you are given a binary tree T=(V,E) with designated root node. In addition, there is an array x[.]with a value for each node in V Define a new array z[.]as follows: for each uV,

z[u]=the maximum of the x-values associated with u’s descendants.

Give a linear-time algorithm that calculates the entire z-array.

Give a linear-time algorithm to find an odd-length cycle in a directed graph. (Hint: First solve this problem under the assumption that the graph is strongly connected.)

In an undirected graph, the degreed(u) of a vertex u is the number of neighbours u is the number of neighbors u has, or equivalently, the number of edges incident upon it. In a directed graph, we distinguish between the indegreedin(u), which is the number of edges into u, and the outdegreedout(u), the number of the edges leaving u.

(a) Show that in an undirected graph, role="math" localid="1658908755010" uevd(u)=2|E|

(b) Use part (a) to show that in an undirected graph, there must be an even number of vertices whose degree is odd.

(c) Does a similar statement hold for the number of vertices with odd indegree in a directed graph?

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free