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

A bipartite graph is a graph G=(V,E)whose vertices can be partitioned into two sets (V=V1V2andV1V2=ϕ) such that there are no edges between vertices in the same set (for instance, if , then there is no edge between and ).

(a) Give a linear-time algorithm to determine whether an undirected graph is bipartite.

(b) There are many other ways to formulate this property. For instance, an undirected graph is bipartite if and only if it can be colored with just two colors. Prove the following formulation:

an undirected graph is bipartite if and only if it contains no cycles of odd length.

(c) At most how many colors are needed to color in an undirected graph with exactly one odd length?

Let S be a finite set. A binary relation on S is simply a collection R of ordered pairs(x,y)S×S. . For instance, S might be a set of people, and each such pair (x,y)R might mean “ x knows y ”.

An equivalence relationis a binary relation which satisfies three properties:

  • Reflexivity: localid="1659006645990" (x,y)R for all XS
  • Symmetry: If (x,y)R then (y,x)R
  • Transitivity: if (x,y)R and (y,z)R then localid="1659006784500" (x,Z)R

For instance, the binary relation “has the same birthday as” is an equivalence relation, whereas “is the father of” is not, since it violates all three properties.

Show that an equivalence relation partition set S into disjoint groups S1,S2,,Sk (in other words, S=S1S2SkandSiSj=ϕforallij ) such that:

  • Any two members of a group are related, that is, (x,y)R for any localid="1659006702579" (x,y)Si, for any i .
  • Members of different groups are not related, that is, for all ij, for all localid="1659006762355" xSi andySi, we have (x,Z)R.

(Hint: Represent an equivalence relation by an undirected graph.)

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?

Perform depth-first search on each of the following graphs; whenever there’s a choice of vertices, pick the one that is alphabetically first. Classify each edge as a tree edge, forward edge, back edge, or cross edge, and give the pre and post number of each vertex.

Give an efficient algorithm which takes as input a directed graph G(V,E)and determines whether or not there is a vertexsV from which all other vertices are reachable.

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