Question

Write a program to test the breadth-first topological ordering algorithm. Data Structures, Visual Studios 2017 C++

Write a program to test the breadth-first topological ordering algorithm.

Data Structures, Visual Studios 2017 C++

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

main.cpp

#include <iostream>

#include <vector>

#include <queue>

using namespace std;

const int vert = 6;

int src;

vector <bool> visitsOfVertex(vert, false);

vector <int> inDeg(vert, 0);

vector <int> X;

void creatingEdges(vector <int> arr[], int u, int v1){

arr[u].push_back(v1);

}

void TopSort(vector <int> arr[]){

queue <int> qu;

int v1;

for (int x = 0; x < vert; x++)

for (vector <int>::iterator iter = arr[x].begin(); iter != arr[x].end(); iter++)

inDeg.at(*iter)++;

for (int x = 0; x < vert; x++)

if (inDeg.at(x) == 0){

src = x;

qu.push(src);

visitsOfVertex.at(src) = true;

break;

}

while (!qu.empty()){

v1 = qu.front();

qu.pop();

X.push_back(v1);

for (vector<int>::iterator iter = arr[v1].begin(); iter != arr[v1].end(); iter++)

if (!visitsOfVertex.at(*iter)){

inDeg.at(*iter)--;

if (inDeg.at(*iter) == 0){

qu.push(*iter);

visitsOfVertex.at(*iter) = true;

}

}

}

}

int main (void){

vector<int> arr[vert];

creatingEdges(arr, 0, 1);

creatingEdges(arr, 0, 3);

creatingEdges(arr, 1, 2);

creatingEdges(arr, 1, 3);

creatingEdges(arr, 2, 3);

creatingEdges(arr, 2, 4);

creatingEdges(arr, 2, 5);

creatingEdges(arr, 3, 4);

creatingEdges(arr, 3, 5);

creatingEdges(arr, 4, 5);

TopSort(arr);

cout << "Topological Ordering is: ";

for (int x = 0; x < X.size(); x++)

cout << X.at(x) << " ";

cout << endl;

}

Output:

Topological Ordering is: 0 1 2 3 4 5

Add a comment
Know the answer?
Add Answer to:
Write a program to test the breadth-first topological ordering algorithm. Data Structures, Visual Studios 2017 C++
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 a program for topological ordering of a graph in c/c++ programming. The input contains a...

    write a program for topological ordering of a graph in c/c++ programming. The input contains a list of adjacent list of a directed acyclic graph. The output is a topological ordering of verticies.

  • 3.3. Run the DFS-based topological ordering algorithm on the following graph. Whenever you have a...

    3.3. Run the DFS-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. (a) Indicate the pre and post numbers of the nodes. (b) What are the sources and sinks of the graph? (c) What topological ordering is found by the algorithm? (d) How many topological orderings does this graph have? 3.3. Run the DFS-based topological ordering algorithm on the following graph. Whenever you have...

  • using C (not C++) in visual studios 2015 write a program that Defines new data type...

    using C (not C++) in visual studios 2015 write a program that Defines new data type “Account” & data members are id_no, balance, and rate. Your program should obtain Account information from the user. Create an Account object, initialize the object and calculate the interest on the account. Output: Enter account id:122655 Enter balance: 3000 Enter intrest rate: 0.1 The intrest on the account #122655 is $300.00

  • Write a program using C in Microsoft visual studios. Write a function that receives an array...

    Write a program using C in Microsoft visual studios. Write a function that receives an array of integers and the array length and prints one integer per line (Hint: Use \n for a line break). Left align all of the integers and use a field width of 10. Populate an array of 10 elements from the user and pass the array and its length to the function.

  • Include the blackbox output done with C# microsoft visual studios software. write a C# program to...

    Include the blackbox output done with C# microsoft visual studios software. write a C# program to sort a parallel array that consists of customer names and customer phone numbers. The solution should be in ascending order of customer names. For instance, if the input is string[] custNames- { "ccc", "ddd", "aaa", "bbb" }; stringl] custIds687-3333", "456-4444", "789-1111", "234-2222" ; then, the solution is string[] string[] custNames- { "aaa", "bbb", "ccc", "ddd" }; custIds"789-1111", "234-2222", "687-3333", "456-4444"]; There are some restrictions:...

  • C++ Write a program that outputs the nodes of a graph in a breadth-first traversal.

    C++ Write a program that outputs the nodes of a graph in a breadth-first traversal.

  • C++ Visual Studios - Program - Vector - Simple Create a program that utilizes VECTOR for...

    C++ Visual Studios - Program - Vector - Simple Create a program that utilizes VECTOR for the following topic. Topic: Carl's Cab Stand needs a program to keep track of their daily clients. Your program shall allow the user to enter 10 names. You will then retrieve the names, one by one, from the data structure (using the appropriate method of retrieval for each data structure) and present them on-screen so that Carl knows who to service next. Include a...

  • programming in Microsoft visual studio. Write a C++ program for the following algorithm. Compile, run, and...

    programming in Microsoft visual studio. Write a C++ program for the following algorithm. Compile, run, and verify the result by choosing some test data. Prompt user to write X1 value in double Read X1 Prompt user to write X2 value in double Read X2 Prompt user to write Y1 value in double Read Y1 Prompt user to write Y2 value in double Read Y2 Compute the lengths of the two sides of the right triangle generated by the two points...

  • C# Visual Studios HelloWorld For this program, you will need to sum the values of an...

    C# Visual Studios HelloWorld For this program, you will need to sum the values of an array and print the sum. A numbers array of integers will be provided for you in the program file. Since the array will already be populated with values, all your program needs to do is calculate the sum of all the values and print the output. Sample output Sum: 215

  • Write a C++ program called ts.cpp that implements the topological sorting algorithm based on the DFS...

    Write a C++ program called ts.cpp that implements the topological sorting algorithm based on the DFS algorithm. Your program should read an input file name and determine if the input graph is a DAG (= directed acyclic graph) or not. If the graph is not a DAG, your program has to stop without further processing. However, if it’s a DAG, your program should display the starting node(s), popping-off order, and topologically sorted list. In the problem, you can assume that...

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