Question

In: Computer Science

In this program you will read a file specifying a directed graph G as a list...

In this program you will read a file specifying a directed graph G as a list of edges. Each edge u →v is listed on one line as u v. The input file simply lists the edges in arbitrary order as pairs of vertices, with each edge on a separate line. The vertices are numbered in order from 1 to the total number of vertices. The program outputs the out-degree sequence for GSCC in increasing order.

For example for the input below:

2 1

3 2

1 3

1 5

3 4

4 5

6 7

5 6

7 4

3 11

2 8

8 9

10 8

9 10

9 4

9 7

10 11

7 11

6 11

the corresponding output is:

0 1 2 3

You should be able to implement this program to run in linear time (time proportional to the total number of vertices plus the total number of edges in the input graph)

Use Kosaraju's Algorithm to find the SCC

Please code this in C

Solutions

Expert Solution

Solution:

Since language of code is not mentioned, I will be using Python
Store the input in file named "input.txt"
Code: Explaination

Store out degree count for each vertex in a list
Copy the out degree count to a set. This will ensure final result will have only distinct values and also set keep those values in increasing order.
Print the result accordingly
file = open('input.txt', 'r')
input_lines = file.readlines()

# Assuming maximum number of vertices are 100
list_outdegree = [0]*100

# split each line of input u and v vertex
for line in input_lines:
edge = line.split(' ')
u = int(edge[0])
v = int(edge[1])

# Add one to out-degree count of u vertex
list_outdegree[u] += 1
  
  
# Set stores distinct number of out-degrees count and store it in increasing order
set_degree = set()
for degree in list_outdegree:
set_degree.add(degree)
  

# print output as required
for d in set_degree:
print(d, end=' ')
Output:

0 1 2 3
Screeshot is attached, to understand indentation used in the python code


Related Solutions

In this program you will read a file specifying a directed graph G as a list...
In this program you will read a file specifying a directed graph G as a list of edges. Each edge u →v is listed on one line as u v. The input file simply lists the edges in arbitrary order as pairs of vertices, with each edge on a separate line. The vertices are numbered in order from 1 to the total number of vertices. The program outputs the out-degree sequence for GSCC in increasing order. For example for the...
C++ program that converts a directed graph data from a user into a corresponding adjacency list...
C++ program that converts a directed graph data from a user into a corresponding adjacency list format. First it will read an input graph data from a user first, after it will convert it to the adjacency matrix format. Assume that the maximum number of vertices in the input graph is less than or equal to 50. The Sample Input 1 3 6 0 1 1 0 1 2 2 1 2 0 0 2 Output 1 0->1->2 1->0->2 2->0->1...
A DAG is a directed graph that contains no directed cycles. Define G = (V, E)...
A DAG is a directed graph that contains no directed cycles. Define G = (V, E) in which V is the set of all nodes as {v1, v2, ..., vi , ...vn} and E is the set of edges E = {ei,j = (vi , vj ) | vi , vj ∈ V} . A topological order of a directed graph G = (V, E) is an ordering of its nodes as {v1, v2, ..., vi , ...vn} so that...
C++, Complete this program as directed // This program will read in a group of test...
C++, Complete this program as directed // This program will read in a group of test scores (positive integers from 1 to 100) // from the keyboard and then calculate and output the average score // as well as the highest and lowest score. There will be a maximum of 100 scores. // PLACE YOUR NAME HERE #include <iostream> using namespace std; typedef int GradeType[100]; // declares a new data type: // an integer array of 100 elements float findAverage...
Question6: Write a program to read the file 202010mid.txt, store the content in a list of...
Question6: Write a program to read the file 202010mid.txt, store the content in a list of tuples, then print the list. [6 marks] Question7: Write Python code to do the following operations using request library. [12 marks] Check if the following webpage is available or not: https://edition.cnn.com/news.html [4 marks] Send a get request to the following webpage and show the result: http://api.open-notify.org/iss-pass.json [4 marks] The webpage from part 2 is expecting some parameters as shown from the results. Now create...
You are given a directed graph G(V,E) with n vertices and m edges. Let S be...
You are given a directed graph G(V,E) with n vertices and m edges. Let S be the subset of vertices in G that are able to reach some cycle in G. Design an O(n + m) time algorithm to compute the set S. You can assume that G is given to you in the adjacency-list representation.
You are required to read in a list of stocks from a text file “stocks.txt” and...
You are required to read in a list of stocks from a text file “stocks.txt” and write the sum and average of the stocks’ prices, the name of the stock that has the highest price, and the name of the stock that has the lowest price to an output file. The minimal number of stocks is 30 and maximal number of stocks in the input file is 50. You can download a input file “stocks.txt” from Canvas. When the program...
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
. Provide a weighted directed graph G = (V, E, c) that includes three vertices a,...
. Provide a weighted directed graph G = (V, E, c) that includes three vertices a, b, and c, and for which the maximum-cost simple path P from a to b includes vertex c, but the subpath from a to c is not the maximum-cost path from a to c
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT