'Trying to find loops in an Undirected Graph
I am trying to write an algorithm that finds out if the vertex I am entering into a Graph is forming a triangle loop with surrounding Vertices. My algorithm however never returns true and I don't know why.
private static bool IsCyclic(UndirectedGraphStructure graphStructure, int vertexA, int vertexB, int startingVertex, List<int> connectionList)
{
//go two steps
//check if you reach the original one again after going two steps
//I want to go through each connection of a given vertex and look at the connections of all the connections of that vertex and if that equals the original then its a loop
// A -> B -> C <= has A in it?
for (int i = 0; i < graphStructure.Adjacent[vertexA].Count; i++)
{
if(graphStructure.Adjacent[vertexA][i].isForward)
{
for (int j = 0; j < graphStructure.Adjacent[graphStructure.Adjacent[vertexA][i].connection].Count; j++)
{
if(graphStructure.Adjacent[graphStructure.Adjacent[vertexA][i].connection][j].isForward)
{
for (int l = 0; l < graphStructure.Adjacent[graphStructure.Adjacent[graphStructure.Adjacent[vertexA][i].connection][j].connection].Count; l++)
{
if(graphStructure.Adjacent[graphStructure.Adjacent[graphStructure.Adjacent[vertexA][i].connection][j].connection][l].isForward)
{
if (graphStructure.Adjacent[graphStructure.Adjacent[graphStructure.Adjacent[vertexA][i].connection][j].connection][l].connection == vertexA)
{
return true;
}
}
}
}
}
}
}
return false;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
