Question

Write a Python or C++ program that takes as input a directed graph and returns a...

Write a  Python or C++ program that takes as input a directed graph and returns a copy of the graph with all edges reversed. You can only assume that you have access to the sequence of nodes and the sequence of edges of the graph. Generate a random graph and test your program on it.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<bits/stdc++.h>
using namespace std;
int vertices = 4 , edges =6;
vector<int>original[4];
vector<int>reversed[4];

// method to store the reversed edges
void reverse_edges(){
for(int i=0;i<vertices;i++){
for(int j=0;j<original[i].size();j++){
int k=original[i][j];
reversed[k].push_back(i);
}
}
}

int main(){
  
// take edges as input from user
  
for(int i=0;i<edges;i++){
int u,v;
cin>>u>>v;
original[u].push_back(v);
}
  
// print the original edges
cout<<"Original edges are as follows : \n";
  
for(int i=0;i<vertices;i++){
for(int j=0;j<original[i].size();j++){
cout<<i<<" -> "<<original[i][j]<<"\n";

}
}
reverse_edges();
  
// print the reversed edges
cout<<"Reversed edges are as follows : \n";
  
for(int i=0;i<vertices;i++){
for(int j=0;j<reversed[i].size();j++){
cout<<i<<" -> "<<reversed[i][j]<<"\n";

}
}
}

Screenshots:

#include<bits/stdc++.h> using namespace std; int vertices = 4 , edges =6; vector int>original[4]; vector<int>reversed[4]; //// print the original edges cout<<Original edges are as follows : \n; for(int i=0;i<vertices; i++) { for(int j=0; j<origina

Output:

Original edges are as follows: -> 1 O-> 2 0 -> 3 1 -> 3 1 -> 2 2 -> 3 w nownReversed edges are as follows: 1 -> o 2 -> o 2 -> 1 3 -> 0 3 -> 1 3 -> 2 wwwNN

Add a comment
Know the answer?
Add Answer to:
Write a Python or C++ program that takes as input a directed graph and returns a...
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
  • write easiest program to under stand:- a) Python program that takes three coefficients (a, b, and...

    write easiest program to under stand:- a) Python program that takes three coefficients (a, b, and c) of a Quadratic equation (ax2+bx+c=0) as input and compute all possible roots b).User defined function isprime(num) that accepts an integer argument and returns 1 if the argument is prime, a 0 otherwise. Write a Python program that invokes this function to generate prime numbers between the given ranges

  • Write a python function that takes a string as input, and returns a dictionary of frequencies...

    Write a python function that takes a string as input, and returns a dictionary of frequencies of all the characters in the string. For example: freq("dabcabcdbbcd") would return {"d":3, "a":2, "b":4, "c":3}. Then write a driver to demonstrate your function.

  • 3. Give an efficient algorithm that takes as input a directed graph G-(V,E) with edges labeled wi...

    Please show your work 3. Give an efficient algorithm that takes as input a directed graph G-(V,E) with edges labeled with either 0 or 1, and vertices s and t that ouputs TRUE if and only if there is a path (not necessarily simple) that goes from s to t such that the binary sequence of edges in the path avoids the substring "11" and outputs FALSE otherwise. (For example, the string 10100010 avoids 11 but the string 00101101110 does...

  • 3. Given a directed graph G < V E >, we define its transpose Gr < V.E1 > to be the graph such tha...

    3. Given a directed graph G < V E >, we define its transpose Gr < V.E1 > to be the graph such that ET-{ < v, u >:< u, v >EE). In other words, GT has the same number of edges as in G, but the directions of the edges are reversed. Draw the transpose of the following graph: ta Perform DFS on the original graph G, and write down the start and finish times for each vertex in...

  • In python 3, 1. Write a program that takes a number of credit hours and returns...

    In python 3, 1. Write a program that takes a number of credit hours and returns a classification as follows: 29 credits or less: freshman; 30-59 credits: sophomore; 60-89 credits: junior; 90+ credits: senior. 2. Use a loop to valid input such that users can only enter positive numbers. 3. Use a loop to allow the user to keep entering credit hours and receiving classifications until they quit. All code must be in a function. I suggest the following structure:...

  • Using Python Programming Language: 3. Write a function flatten that takes a 2D list and returns...

    Using Python Programming Language: 3. Write a function flatten that takes a 2D list and returns all the items of each list concatenated together into one new 1D list. For example: flatten ([["a", "b"],["c","0"],["e","f"]]) would return ["a", "b","C","d", "e","f"] Save the function in a PyDev library module named functions.py Write a program t03.py that tests flatten function and prints the returned flat list to the screen. Test your program with a different list, hardcoded in t03.py • Copy the results...

  • C Program: Write the following function that takes a user input string, computes, and determines whether the input is palindromic or not by returning either true or false:

    A palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or reversed. Examples: “Dad”, “Anna”, “Never odd or even”, etc. For this problem, we will only consider a string to be palindromic if its combined alpha-numeric characters (‘A’-’Z’, ‘a’-’z’, and ‘0’-’9’) can be read the same both forward and backward, by skipping all other non-alphanumeric characters.Write the following function that takes a user input string, computes, and determines whether the input is palindromic or not...

  • IN PYTHON Write a program that takes a user-input distance and speed and then calculates an...

    IN PYTHON Write a program that takes a user-input distance and speed and then calculates an ETA. Your program should work for ANY COMBINATION of distance and speed.

  • python Write a function that takes as input a single integer parametern and computes the nth...

    python Write a function that takes as input a single integer parametern and computes the nth Fibonacci Sequence number The Fibonacci sequence first two terms are 1 and 1(so, if n - 2 your function should return 1). From there, the previous two values are summed to come up with the next result in the sequence 1.1,2,3,5,8,13, 21, 34, 55, etc.) For example, if the input is 7 then your function should return 13 26561.1615880 1 def Fibonacci(n): # your...

  • Write a Java program that takes an int array as input, and returns the average of...

    Write a Java program that takes an int array as input, and returns the average of all the values in the array.  No class construction is necessary; however, the complete signature and definition and brief documents are needed.  

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