Question

2. Reverse of a digraph 30 Marks For a given set of digraphs, write a program that prints out the reverse of each digraph Inp

Reverse of a digraph 30 Marks

For a given set of digraphs, write a program that prints out the reverse of each digraph. Input format: described below under the heading “Digraph input format”. Note that adjacency lists are sorted. Output format: use the input format to output your result ensuring that the output adjacency lists are sorted.

the input format:

4

1. 3

2. 3

0

3

1. 2

1

0

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

The reverse of a directed graph is a graph in which the edges are reversed in direction as compared to the original graph. That is if there is an edge (u, v) which represents an edge directed from u to v, then in the reverse of the directed graph, we would have an edge (v, u). The algorithm to find out the reverse of a directed graph is fairly simple. For every node, we traverse that nodes edge set and for every edge set member (ui , vi) we reverse its direction to (vi , ui). The algorithm is fairly simple in this problem but the way the input graph is provided is a bit tricky, but I will try to answer that too as well. I have used python to answer this question and if you have any problem understanding the code ask me in the comment section and I will definitely help you out. Please do remember to input the graph in the exact way the input is being specified in the problem, That is first input the size of the graph first, then followed by the N lines describing the edge set, which includes space separated numbers that indicate the nodes to which the ith node is connected to. Please remember to input an empty line to nodes from which there are no outgoing edges, the code heavily depends on the input section of the problem. I will provide you with the way the input is to be provided so that the desired output is obtained.

Input:-

4
1 3
2 3
0

3
1 2

1
0

Code (in python 2.7):-

while(1):
   n = input()
   if(n == 0):
       print (0)
       break
   print (n)
   ori_graph = []
   rev_graph = []
   for i in range(0, n):
       ori_graph.append([])
       rev_graph.append([])
   for i in range(0, n):
       ar = raw_input().split()
       for j in range(0, len(ar)):
           ori_graph[i].append(int(ar[j]))
   for i in range(0, n):
       for j in range(0, len(ori_graph[i])):
           rev_graph[ori_graph[i][j]].append(i)
   for i in range(0, n):
       rev_graph[i].sort()
       for j in range(0, len(rev_graph[i])):
           print rev_graph[i][j],
       print ('')

If you are having any kind of problem in understanding the code, related to the algorithm used to reverse the graph, or the input format used feel free to ask me in the comments. I am there to help you out here. Thank you.

Add a comment
Know the answer?
Add Answer to:
Reverse of a digraph 30 Marks For a given set of digraphs, write a program that prints out the reverse of each digraph. Input format: described below under the heading “Digraph input format”. Note th...
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
  • plz use python to answer it, thanks in advance 3. Tree and back arcs in a DFS 40 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints...

    plz use python to answer it, thanks in advance 3. Tree and back arcs in a DFS 40 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints out the total number of tree arcs and back arcs resulting from the traversal. Use our standard convention that when there is a choice of white or grey nodes, the one with the lowest index should be chosen. Input format:...

  • Write a C program which calculates how many digits a number has and prints each digit...

    Write a C program which calculates how many digits a number has and prints each digit in reserve order with space in between(no space after the last digit). The number > 0 and has no more than 5 digits. (optional] It is better to write two functions. One is designed for printing number in reverse order and the other is for counting the number of digits. Given that the value of number (which is an int variable) is 12345, the...

  • C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints...

    C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints out the answer based on arithmetic. Assume that there are no overflow, underflow and division by zero cases. Your function should be named calculator Your function takes three input parameter: two double numbers and one char operator Your function does not return anything Your function prints answer in the format specified below Your function should set precision point to 2 Note: You must use...

  • String Processing Labs Directions: Write a main program to test the three functions described below, Input...

    String Processing Labs Directions: Write a main program to test the three functions described below, Input is from a file, and output is to a file. 1. The function Another parameter is a char variable with a letter value. The function outputs the number of times the char value appears in the string. processes a string containing letters of the alphabet, which is a parameter 2. This Boolean function has two string parameters. It returns true when the first str...

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.t...

    Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.txt) which you will create from the data provided below. The program will then prompt the user for a name to search for, when it finds the name it will output to a file (call it OFile.txt) the person’s id#, name,...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. This program needs to use Switch Case in order to call the methods and format to print each month. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:      ...

  • This program uses C++. This program reads in a line from the user and prints it...

    This program uses C++. This program reads in a line from the user and prints it out in a certain format. An example would be Input: 1 2 3 4 5 would result Output: [{1}, {2}, {3}, {4}, {5}]. When quotations marks are added into the input the format becomes different. For instance, Input 1 2 "3 4 5" would result in [{1}, {2}, {3 4 5}]. When I ad multiple quotation marks into the input, it will only use...

  • Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out...

    Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out the sum of the two numbers in hexadecimal. (As noted in class, first do this without using a file and by reading using the cin > > command) From Wikipedia: "In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:             0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...

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