Question
#include <bits/stdc++.h>
using namespace std;
int cur=0;
int G[10][10];
bool visited[10];
deque <int> q;
void fun(int n);
int main()
{
int num=0;
int n;
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>G[i][j];
for(int i=0;i<n;i++)
visited[i]=false;
fun(n);
return 0;
}
void fun(int n)
{
cout<<cur<<" ";
visited[cur]=true;
q.push_back(cur);
do
{
for(int j=0;j<n;j++)
{
if(G[cur][j]==1 && !visited[j])
{
q.push_back(j);
cout<<j<<" ";
visited[j]=true;
}
}
q.pop_front();
if(!q.empty())
cur=q.front();
}while(!q.empty());
}
Input Sequence:-
9
0 1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 1
0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 0 0 1 0 1 1
0 0 0 0 1 0 1 0 0
1 0 1 0 0 0 1 0 0
a.
2
b.
6
c.
8
d.
4
Posted under Data Structures and Algorithms
Engage with the Community - Add Your Comment
Confused About the Answer? Ask for Details Here.
Know the Explanation? Add it Here.
Q. Given the following program, what will be the 3rd number that’d get printed in the output sequence for the given input?
Similar Questions
Discover Related MCQs
Q. Given the following adjacency matrix of a graph(G) determine the number of components in the G.
[0 1 1 0 0 0],
[1 0 1 0 0 0],
[1 1 0 0 0 0],
[0 0 0 0 1 0],
[0 0 0 1 0 0],
[0 0 0 0 0 0].
View solution
Q. Incidence matrix and Adjacency matrix of a graph will always have same dimensions?
View solution
Q. The column sum in an incidence matrix for a simple graph is __________
View solution
Q. What are the dimensions of an incidence matrix?
View solution
Q. The column sum in an incidence matrix for a directed graph having no self loop is __________
View solution
Q. Time complexity to check if an edge exists between two vertices would be ___________
View solution
Q. The graphs G1 and G2 with their incidences matrices given are Isomorphic.
e1 e2 e3 e4 e5 e6
v1 1 0 0 0 0 0
v2 1 1 0 0 0 1
v3 0 1 1 0 1 0
v4 0 0 1 1 0 0
v5 0 0 0 1 1 1
e1 e2 e3 e4 e5 e6
v1 0 0 1 0 0 0
v2 1 0 1 0 1 0
v3 1 1 0 1 0 0
v4 0 1 0 0 0 1
v5 0 0 0 1 1 1
View solution
Q. If a connected Graph (G) contains n vertices what would be the rank of its incidence matrix?
View solution
Q. A Graph Structured Stack is a _____________
View solution
Q. If a Graph Structured Stack contains {1,2,3,4} {1,5,3,4} {1,6,7,4} and {8,9,7,4}, what would be the source and sink vertices of the DAC?
View solution
Q. Graph Structured Stack finds its application in _____________
View solution
Q. If in a DAG N sink vertices and M source vertices exists, then the number of possible stacks in the Graph Structured Stack representation would come out to be N*M.
View solution
Q. Space complexity for an adjacency list of an undirected graph having large values of V (vertices) and E (edges) is ___________
View solution
Q. For some sparse graph an adjacency list is more space efficient against an adjacency matrix.
View solution
Q. Time complexity to find if there is an edge between 2 particular vertices is _________
View solution
Q. For the given conditions, which of the following is in the correct order of increasing space requirement?
i) Undirected, no weight
ii) Directed, no weight
iii) Directed, weighted
iv) Undirected, weighted
View solution
Q. Space complexity for an adjacency list of an undirected graph having large values of V (vertices) and E (edges) is __________
View solution
Q. In which case adjacency list is preferred in front of an adjacency matrix?
View solution
Q. To create an adjacency list C++’s map container can be used.
View solution
Q. Given a plane graph, G having 2 connected component, having 6 vertices, 7 edges and 4 regions. What will be the number of connected components?
View solution
Suggested Topics
Are you eager to expand your knowledge beyond Data Structures and Algorithms? We've curated a selection of related categories that you might find intriguing.
Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!