//Depth First Search Traversal
#include <stdio.h>
#include <memory.h>
#define FIN "graph.in"
#define FOUT "bfs.out"
#define DIM 30
void dfs(int mat[][DIM], int nodes, int startNode) {
int Stack[DIM],
explored[DIM],
ptr, //pointer to the stack
node,
found, k;
memset(explored
, 0, sizeof(explored
));
explored[startNode] = 1;
ptr = 0;
Stack[ptr] = startNode;
printf("%s\n", "Depth First Search");
while(ptr>=0){
node = Stack[ptr];
found = 0;
for(k = 0; !found && k < nodes;++k)
if(mat[node][k] == 1 && !explored[k]) found = 1;
if(!found) {
ptr--;
} else {
explored[k-1] = 1;
Stack[++ptr] = k-1;
}
}
}
int main(int argc, char const *argv[]) {
int matrix[DIM][DIM], nodes, i, j;
//freopen(FIN, "r", stdin);
int startNode;
//freopen(FOUT, "w", stdout);
for(i = 0; i < nodes; ++i) {
for(j = 0; j < nodes; ++j) {
scanf("%d", &matrix
[i
][j
]); }
}
//Display matrix adjcency
for(i = 0; i < nodes; ++i) {
for(j = 0; j < nodes; ++j) {
}
}
startNode = 0;
dfs( matrix, nodes, startNode );
return 0;
}
Ly9EZXB0aCBGaXJzdCBTZWFyY2ggVHJhdmVyc2FsCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8bWVtb3J5Lmg+CiNkZWZpbmUgRklOICJncmFwaC5pbiIKI2RlZmluZSBGT1VUICJiZnMub3V0IgojZGVmaW5lIERJTSAzMAoKdm9pZCBkZnMoaW50IG1hdFtdW0RJTV0sIGludCBub2RlcywgaW50IHN0YXJ0Tm9kZSkgewoKICAgICBpbnQgU3RhY2tbRElNXSwKICAgICAgICAgZXhwbG9yZWRbRElNXSwKICAgICAgICAgcHRyLCAvL3BvaW50ZXIgdG8gdGhlIHN0YWNrCiAgICAgICAgIG5vZGUsCiAgICAgICAgIGZvdW5kLCBrOwoKICAgICBtZW1zZXQoZXhwbG9yZWQsIDAsIHNpemVvZihleHBsb3JlZCkpOwoKICAgICBleHBsb3JlZFtzdGFydE5vZGVdID0gMTsKCiAgICAgcHRyID0gMDsKCiAgICAgU3RhY2tbcHRyXSA9IHN0YXJ0Tm9kZTsKCiAgICAgcHJpbnRmKCIlc1xuIiwgIkRlcHRoIEZpcnN0IFNlYXJjaCIpOwoKICAgICBwcmludGYoIiVkICIsIHN0YXJ0Tm9kZSsxKTsKCiAgICAgd2hpbGUocHRyPj0wKXsKCiAgICAgICAgIG5vZGUgPSBTdGFja1twdHJdOwoKICAgICAgICAgZm91bmQgPSAwOwoKICAgICAgICAgZm9yKGsgPSAwOyAhZm91bmQgJiYgayA8IG5vZGVzOysraykKCiAgICAgICAgICAgICBpZihtYXRbbm9kZV1ba10gPT0gMSAmJiAhZXhwbG9yZWRba10pIGZvdW5kID0gMTsKCiAgICAgICAgICBpZighZm91bmQpIHsKCiAgICAgICAgICAgICAgcHRyLS07CgogICAgICAgICAgfSBlbHNlIHsKCiAgICAgICAgICAgICAgcHJpbnRmKCIlZCAiLCBrKTsKICAgICAgICAgICAgICBleHBsb3JlZFtrLTFdID0gMTsKICAgICAgICAgICAgICBTdGFja1srK3B0cl0gPSBrLTE7CiAgICAgICAgICB9CgogICAgIH0KCiAgICAgcHJpbnRmKCJcbiIpOwp9CgppbnQgbWFpbihpbnQgYXJnYywgY2hhciBjb25zdCAqYXJndltdKSB7CgogIGludCBtYXRyaXhbRElNXVtESU1dLCBub2RlcywgaSwgajsKCiAgLy9mcmVvcGVuKEZJTiwgInIiLCBzdGRpbik7CiAgaW50IHN0YXJ0Tm9kZTsKICAvL2ZyZW9wZW4oRk9VVCwgInciLCBzdGRvdXQpOwogIHNjYW5mKCIlZCIsICZub2Rlcyk7CgogIGZvcihpID0gMDsgaSA8IG5vZGVzOyArK2kpIHsKICAgIGZvcihqID0gMDsgaiA8IG5vZGVzOyArK2opIHsKICAgICAgc2NhbmYoIiVkIiwgJm1hdHJpeFtpXVtqXSk7CiAgICB9CiAgfQoKICAvL0Rpc3BsYXkgbWF0cml4IGFkamNlbmN5CgogIGZvcihpID0gMDsgaSA8IG5vZGVzOyArK2kpIHsKICAgIGZvcihqID0gMDsgaiA8IG5vZGVzOyArK2opIHsKICAgICAgcHJpbnRmKCIlZCAiLCBtYXRyaXhbaV1bal0pOwogICAgfQogICAgcHJpbnRmKCJcbiIpOwogIH0KCiAgc3RhcnROb2RlID0gMDsKCiAgZGZzKCBtYXRyaXgsIG5vZGVzLCBzdGFydE5vZGUgKTsKCiAgcmV0dXJuIDA7Cn0=