Please provide C language code no c++ ,txt file also needed
with comment
Finish task 5
All the explanation is in the code comments. Hope this helps!
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
using namespace std;
int main(int argc, char** argv)
{
// open file
// ifstream fin("graph.txt");
// for command line inputfile, use the following
ifstream fin(argv[1]);
// read number of nodes
int n, node, edge;
string edges;
fin >> n;
// create adjacency list for graph
vector<int> adj[n];
char c;
// start reading the lines
for(int i=0; i<n; i++)
{
// read node first
fin >> node;
// read the current line
getline(fin, edges);
// split the line into required data
for(int i=0; i<edges.size(); i++)
{
if(edges[i] == ' ')
edge = 0; // prepare for new number
else if(edges[i] == ',')
adj[node-1].push_back(edge-1); // end of the number
else
edge = edge * 10 + (int) (edges[i] - '0'); // add digit to
number
}
// last edge
adj[node-1].push_back(edge-1);
}
// close file
fin.close();
// start the bfs
// Mark all the vertices as not visited
bool visited[n];
// also find the starting node
int s = 0;
for(int i = 0; i < n; i++)
{
visited[i] = false;
// starting node is the node with maximum degree
if(adj[i].size() >= adj[s].size())
s = i;
}
// Create a queue for BFS
list<int> queue;
// Mark the current node as visited and enqueue it
visited[s] = true;
queue.push_back(s);
cout << "BFS:";
while(!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << " " << (s+1);
queue.pop_front();
// Get all adjacent vertices of the dequeued vertex s.
// visit the adjacency node in opposite order
// for the highest child has the most priority
// If a adjacent has not been visited, then mark it visited and
enqueue it
for (int i = adj[s].size()-1; i >= 0; --i)
{
if (!visited[adj[s][i]])
{
visited[adj[s][i]] = true;
queue.push_back(adj[s][i]);
}
}
}
cout << endl;
return 0;
}
Sample output:
input
output
Code screenshot
Please provide C language code no c++ ,txt file also needed with comment Finish task 5...
JAVA LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents the graph. Your code should contain a function called addEdgelint i, int j). Use this function to add the appropriate edges in your matrix. Write code to implement Depth-First-Search (DFS) and Breadth-First-Search (BFS) of your graph. Let 0 be the source Node . Traverse the graph using DFS, print the Nodes as they are visited. . Traverse the...
please help Programming: Undirected Graphs 10. Run the BFS algorithm on this graph to compute the shortest paths between 0 and every other node. For reference, the BFS algorithm is shown on the next page. Use the adjacency list above for the order of the nodes explored and follow the trace format shown before. Your answer must include the values of v, queue, and edge To, as they update. [20 point s 0 3, 1 10, 4, 3, 2 21.5...
In Python 3 please Apply Breadth First Search (BFS) to traverse the following graph. Start your traversal from vertex 0, and write down the order in which vertices will be visited during the traversal. 1 8 6 7 2 9 5 4 3
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...
Please answer all three parts. And show step-by-step answers for each part. Draw anything if necessary. And please don't copy other answers to be at risk being downvoted. Thank you. Question 1 (50 POINTS): Given a graph G and the Breadth First Search (BFS) and Depth First Search (DFS) traversal algorithms as follows: BFSG) 1 for each vertex u € G.V – {3} 1 2 u.color = WHITE 3 u.d = 0 4 un = NIL 3 5 S.color =...
JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to...
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:...
Creating a simple graph in C++; need solution ASAP. EDIT: Pls comment letting me know what other information you need rather than just "..." Thank you. Here is the assignment: In this final practice problem, you’ll: read a set of data representing a directed, unweighted graph build an in-memory graph structure using the data display the graph using depth-first traversal display the graph using breadth-first traversal Input data - The data consists of records like this: 16 3 15 4...
Consider an input file A1.txt. Each line of A1.txt consists of two numbers separated by space. Write a C++ program that reads the two numbers from each line and adds all the integers between those two numbers (starting from the first number and ending at the second. Show your output in B1.txt. Sample Input: 1 6 18 74 29 38 Sample Output: 21 2622 335 (For further clarification, the first line of the sample output was done by 1+2+3+4+5+6 =...
QUESTION 1 In a tree, a ____ is a node with successor nodes. root child descendent parent sibling QUESTION 2 In a tree, the ____ is a measure of the distance from a node to the root. count degree branch height level QUESTION 3 Which of the following is not a characteristic of a binary search tree? Each node has zero, one, or two successors. The preorder traversal processes the node first, then the left subtree, and then the right...