Network Loop Detection System
Documentation
1. System Overview
A C# implementation for detecting loops in network topologies using:
- Switch-based network representation
- Depth-First Search (DFS) algorithm
- Loop path reconstruction
2. Complete Code Solution
Below is the C# implementation for the network loop detection system:
using System;
using System.Collections.Generic;
namespace NetworkLoopDetection
{
public class Network
{
private int switchCount;
private List<Tuple<int, int>> cables;
private List<int>[] adjacencyList;
public Network(int switches)
{
switchCount = switches;
cables = new List<Tuple<int, int>>();
adjacencyList = new List<int>[switches];
for (int i = 0; i < switches; i++)
adjacencyList[i] = new List<int>();
}
public void Connect(int switchA, int switchB)
{
if (switchA < switchB)
cables.Add(Tuple.Create(switchA, switchB));
else
cables.Add(Tuple.Create(switchB, switchA));
adjacencyList[switchA].Add(switchB);
adjacencyList[switchB].Add(switchA);
}
public LoopCheckResult CheckForLoops()
{
bool[] visited = new bool[switchCount];
int[] parent = new int[switchCount];
Array.Fill(parent, -1);
for (int i = 0; i < switchCount; i++)
if (!visited[i])
{
var result = DFS(i, visited, parent);
if (result.HasLoop)
return result;
}
return new LoopCheckResult(false, null);
}
private LoopCheckResult DFS(int current, bool[] visited, int[] parent)
{
visited[current] = true;
foreach (int neighbor in adjacencyList[current])
{
if (!visited[neighbor])
{
parent[neighbor] = current;
var result = DFS(neighbor, visited, parent);
if (result.HasLoop) return result;
}
else if (neighbor != parent[current])
{
List<int> path = ReconstructPath(current, neighbor,
parent);
return new LoopCheckResult(true, path);
}
}
return new LoopCheckResult(false, null);
}
private List<int> ReconstructPath(int start, int end, int[] parent)
{
List<int> path = new List<int>();
int node = start;
while (node != end)
{
path.Add(node);
node = parent[node];
}
path.Add(end);
path.Add(start);
path.Reverse();
return path;
}
public void PrintConnections()
{
Console.WriteLine("Network Connections:");
foreach (var (a, b) in cables)
Console.WriteLine($"Switch {a} <--> Switch {b}");
}
}
public class LoopCheckResult
{
public bool HasLoop { get; }
public List<int> LoopPath { get; }
public LoopCheckResult(bool hasLoop, List<int> path)
{
HasLoop = hasLoop;
LoopPath = path;
}
}
class Program
{
static void Main()
{
Console.WriteLine("Network Configuration Tool");
// Example usage
Network network = new Network(4);
network.Connect(0, 1);
network.Connect(1, 2);
network.Connect(2, 3);
network.Connect(3, 0);
network.PrintConnections();
var result = network.CheckForLoops();
if (result.HasLoop)
Console.WriteLine($"
LOOP DETECTED: {string.Join(" → ", result.LoopPath)}");
else
Console.WriteLine("
No loops detected");
}
}
}
3. Documentation Sections
3.1 Class Structure
Class Responsibility
Network Manages network topology and loop
detection
LoopCheckResult Stores loop detection results
Program Example usage and demonstration
3.2 Key Methods
Method Functionality
Connect() Adds connections between switches
CheckForLoops() Initiates loop detection
DFS() Depth-First Search implementation
ReconstructPath() Builds loop path when found
3.3 Data Structures
Structure Purpose
List<Tuple<int, int>> Stores all cable connections
List<int>[] Adjacency list for graph traversal
bool[] visited Tracks visited nodes in DFS
int[] parent Records traversal path for loop
reconstruction
4. How to Use
Initialize Network:
Network network = new Network(4); // 4 switches
Add Connections:
network.Connect(0, 1); // Connect switch 0 to 1
Check for Loops:
var result = network.CheckForLoops();
if (result.HasLoop)
Console.WriteLine($"Loop path: {string.Join(" → ", result.LoopPath)}");
5. Expected Output
For a looped network (0-1-2-3-0):
Network Connections:
Switch 0 <--> Switch 1
Switch 1 <--> Switch 2
Switch 2 <--> Switch 3
Switch 0 <--> Switch 3
LOOP DETECTED: 0 → 1 → 2 → 3 → 0
For a loop-free network:
No loops detected
6. Additional Notes
Time Complexity: O(V + E) where V = switches, E = cables
Space Complexity: O(V) for DFS storage
Limitations: Designed for undirected networks only