forked from dubey-harshit/Hacktoberfest-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_star_graph.cpp
More file actions
53 lines (44 loc) · 1.02 KB
/
check_star_graph.cpp
File metadata and controls
53 lines (44 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// CPP to find whether given graph is star or not
#include<bits/stdc++.h>
using namespace std;
// define the size of incidence matrix
#define size 4
// function to find star graph
bool checkStar(int mat[][size])
{
// initialize number of vertex
// with deg 1 and n-1
int vertexD1 = 0, vertexDn_1 = 0;
// check for S1
if (size == 1)
return (mat[0][0] == 0);
// check for S2
if (size == 2)
return (mat[0][0] == 0 && mat[0][1] == 1 &&
mat[1][0] == 1 && mat[1][1] == 0 );
// check for Sn (n>2)
for (int i = 0; i < size; i++)
{
int degreeI = 0;
for (int j = 0; j < size; j++)
if (mat[i][j])
degreeI++;
if (degreeI == 1)
vertexD1++;
else if (degreeI == size-1)
vertexDn_1++;
}
return (vertexD1 == (size-1) &&
vertexDn_1 == 1);
}
// driver code
int main()
{
int mat[size][size] = { {0, 1, 1, 1},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0}};
checkStar(mat) ? cout << "Star Graph" :
cout << "Not a Star Graph";
return 0;
}