#include<stdlib.h>
#include<stdio.h>
#define MAX 100
struct edge
//TO STORE EDGE AND WEIGHT OF GRAPH
{
int x;
int y;
int weight;
struct edge *link;
}*front = NULL;
int Print(int a,int Arr[],int k);
void create_tree(int V,struct edge tree[]);
void insert_queue(int i, int j, int wt);
struct edge *delete_queue();
int isEmpty();
int main()
{
int V;
int i=0,j=0;
scanf("%d",&V);
int A[V][V];
//Array for storing the
values of all edges
for(i=0;i<V;i++)
for(j=0;j<V;j++)
{
scanf("%d",&A[i][j]);
if( A[i][j]!=-1
&& i<j )
//Inserting edges
insert_queue(i,j,A[i][j]);
}
struct edge tree[MAX];
int tree_weight = 0;
create_tree(V,tree);
//creating
tree
int k=0,Arr[V],a,b;
for(i=1; i<=V-1; i++)
//storing all vertices in spanning tree
{
//also calculating total cost
a=tree[i].x;
k=Print(a,Arr,k);
b=tree[i].y;
k=Print(b,Arr,k);
tree_weight = tree_weight + tree[i].weight;
}
for(i=0;i<k;i++)
//printing the edges in spanning tree
printf("%d ",Arr[i]);
printf("\n%d\n", tree_weight);
//printing total cost or weight
return 0;
}
void create_tree(int V,struct edge tree[])
//function for creating
tree
{
struct edge *tmp;
int y1, y2, root_y1, root_y2;
int parent[MAX];
int i, count = 0;
for(i = 0; i < V; i++)
{
parent[i] = -1;
}
while(!isEmpty( ) && count < V - 1)
{
tmp = delete_queue();
y1 = tmp->x;
y2 = tmp->y;
while(y1 != -1)
{
root_y1 = y1;
y1 = parent[y1];
}
while(y2 != -1)
{
root_y2 = y2;
y2 = parent[y2];
}
if(root_y1 != root_y2)
{
count++;
tree[count].x = tmp->x;
tree[count].y = tmp->y;
tree[count].weight = tmp->weight;
parent[root_y2] = root_y1;
}
}
}
void insert_queue(int i, int j, int wt)
//function
for storing edges and weight
{
struct edge *tmp, *q;
tmp = (struct edge *)malloc(sizeof(struct edge));
tmp->x = i;
tmp->y = j;
tmp->weight = wt;
if(front == NULL || tmp->weight < front->weight)
{
tmp->link = front;
front = tmp;
}
else
{
q = front;
while(q->link != NULL && q->link->weight <=
tmp->weight)
{
q = q->link;
}
tmp->link = q->link;
q->link = tmp;
if(q->link == NULL)
{
tmp->link = NULL;
}
}
}
int Print(int a,int Arr[],int k)
//function
for storing vertices of spanning tree
{
int i=0,j=0;
for(i=0;i<k;i++)
{
if(Arr[i]==a)
return k;
}
Arr[k++]=a;
return k;
}
struct edge *delete_queue()
//function
for delete node
{
struct edge *tmp;
tmp = front;
front = front->link;
return tmp;
}
int isEmpty()
//function to check the
list
{
if(front == NULL)
return 1;
else
return 0;
}
THE INPUT OUTPUT EXAMPLE IS AS FOLLOWS:-
[INPUT] First line number of vertex v adjacency matrix of graph Second v+1 line (If not connected, 1 If connected, weig...
ignore red marks. Thanks 10. (16) You will compute the strongly connected components of this graph in three steps. a. STRONGLY-CONNECTED-COMPONENTS (G) (7) Perform a depth-first search on call DFS(G) to compute finishing times w/ for each vertex the following graph. (To make 2 compute GT this easier to grade, everyone call DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing wf (as computed in line 1) please start with vertex "a" and 4...
1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching (DFS) by stack (define it with class) to traverse the graph. 6 7 2 4 b. If starting with node 2, when node 7 is printed, what numbers are in the stack (for DFS)? Please draw the stack step by step to show how the numbers are pushed into and popped out of it. 2. a. Given a set of integer numbers as int...
8, (10 pts) Show that given a directed graph G = (V,E) already stored in adjacency matrix form, determining if there is a vertex with in-degree n - 1 and out-degree 0 can be done in O(n) time where n is the number of vertices in V. 8, (10 pts) Show that given a directed graph G = (V,E) already stored in adjacency matrix form, determining if there is a vertex with in-degree n - 1 and out-degree 0 can...
Problem 3's picture are given below. 5. (a) Let G = (V, E) be a weighted connected undirected simple graph. For n 1, let cycles in G. Modify {e1, e2,.. . ,en} be a subset of edges (from E) that includes no Kruskal's algorithm in order to obtain a spanning tree of G that is minimal among all the spanning trees of G that include the edges e1, e2, . . . , Cn. (b) Apply your algorithm in (a)...
Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, to implement the depth-first search algorithm using the pseudocode given. Write a driver program, which reads input file mediumG.txt as an undirected graph and runs the depth-first search algorithm to find paths to all the other vertices considering 0 as the source. This driver program should display the paths in the following manner: 0 to ‘v’: list of all the vertices traversed to...
You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a graph stored as an adjacency list. The AdjacencyList class inherits from the Graph class shown below. class Graph { private: vector _distances; vector _previous; public: Graph() { } virtual int vertices() const = 0; virtual int edges() const = 0; virtual int distance(int) const = 0; virtual void bfs(int) const = 0; virtual void dfs(int) const = 0; virtual void display() const = 0;...
(a) Given a graph G = (V, E) and a number k (1 ≤ k ≤ n), the CLIQUE problem asks us whether there is a set of k vertices in G that are all connected to one another. That is, each vertex in the ”clique” is connected to the other k − 1 vertices in the clique; this set of vertices is referred to as a ”k-clique.” Show that this problem is in class NP (verifiable in polynomial time)...
C++ programing question22 Minimum spanning tree Time limit: 1 second Problem Description For a connected undirected graph G = (V, E), edge e corresponds to a weight w, a minimum weight spaning tree can be found on the graph. Into trees. Input file format At the beginning, there will be a positive integer T, which means that there will be T input data. The first line of each input has two positive integers n,m, representing n points and m edges...
o Write a function that outputs even numbers less than the input number. • Function name should be 'evenprint'. • A number is given as input to the function. . The function must return a list of even numbers def evenprint(num): #write statement >[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74,...
Problem definition: Give the program that implement Prim’s algorithm. Input: First line is N, denotes the amount of test case, then there are Ns graph data with an option number (determine whether output the selected edges or not). Each graph is undirected and connected, it is composed of V (the number of vertices, <= 1000), E (the number of edges, <=10000), then followed by Es edges which are denoted by pair of vertex and weight (e.g., 2 4 10 means...