I have a program that accepts an adjacency matrix as input and then computes whether the corresponding graph is a tree. I want to modify it so that it determines whether the graph is specifically a binary tree, but I really can't wrap my head around it. What do I need to do?
Here's the original code:
#include<stdio.h>
#include<stdlib.h>
int A[20][20]; // Our two-dimensional array.
int visited[20];
int v = 0;
int count = 0;
int n;
int seq[20];
int s = 0;
int connected = 1;
int acyclic = 1;
// Declaration of the DFS function
void DFS();
// Declaration of the DFSearch function
void DFSearch(int cur);
int main() {
int i,j;
printf("\nStep 1 of 2 — Fill in your desired number of vertices: ");
scanf("%d",&n);
printf("\nStep 2 of 2 — Fill in the adjacency matrix(1/0):\n");
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++) {
scanf("%d",&A[i][j]);
}
}
printf("\nThe DFS traversal shows:\n");
DFS();
for(i=1; i<=n; i++) {
printf("%c,%d\t", 'a'+seq[i]-1, i);
}
if(connected && acyclic) {
printf("\n\nThis is a connected and non-cyclic graph. Therefore, it IS a tree.");
}
if(!connected && acyclic) {
printf("\n\nThis is a non-connected and non-cyclic graph. Therefore, it is NOT a tree.");
}
if(connected && !acyclic) {
printf("\n\nThis is a connected and cyclic graph. Therefore, it is NOT a tree.");
}
if(!connected && !acyclic) {
printf("\n\nThis is a non-connected and cyclic graph. Therefore, it is NOT a tree.");
}
printf("\n\n");
return 0;
}
// Definition of the DFS function
void DFS() {
int i;
for(i=1; i<=n; i++) {
if(!visited[i]) {
if(i>1) {
connected = 0;
DFSearch(i);
}
}
}
}
// Definition of the DFSearch function
void DFSearch(int cur) {
int i,j;
visited[cur]=++count;
seq[count]=cur;
for(i=1; i<count-1; i++) {
if(A[cur][seq[i]]) {
acyclic = 0;
}
}
for(i=1; i<=n; i++) {
if(A[cur][i] && !visited[i]) {
DFSearch(i);
}
}
}