Please kindly identify my error in this code. It is not giving the required output and prescribed by the question:
Cycles
Is a graph denoted by the adjacency matrix a cycle graph?
Input
In the first line: a number of next lines.
In a line (expect the first): a number of vertices and after space an adjacency matrix written from the left to the right and from the top to the bottom.
I haven't tried too hard to trace through your code, but it looks like you do not have a “visited” flag — you appear to be trying to reuse the edge value for that... which won’t work.
The simplest solution for your code would be to simply keep an additional array of “nodes visited”:
1 2
bool graph[100][100]; // Directed graph as an adjacency matrix
bool visited[100]; // Was node visited?
Now you should be looking at a DFS to run through the graph, making sure to set a node as visited when you encounter it and to clear the node as unvisited when you are back-tracking.
Remember, this is a recursive algorithm! You need a FUNCTION.
(You could certainly write it iteratively, but don’t.)