방향성에 따라
단방향 그래프 (Directed Graph)

양방향 그래프 (Undirected Graph)

https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
그래프 내의 각 정점의 인접 관계를 나타내는 2차원 배열 구조
bool[,] matrix = new bool[V, V]matrix[i, j] == true → 정점 i에서 정점 j로 연결된 간선이 존재public class AdjacencyMatrixGraph
{
public bool[,] graph = new bool[8, 8];
public void TwoWayGraph(int a, int b)
{
graph[a, b] = true;
graph[b, a] = true;
}
public void OneWayGraph(int a, int b)
{
graph[a, b] = true;
}
public void PrintGraph()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Console.Write($"{(graph[i, j] ? "1" : "")}\t");
}
Console.WriteLine();
}
}
}
그래프의 각 정점에 연결된 이웃 정점들을 리스트로 저장하는 방식
배열 형태:List<int>[] graph = new List<int>[V]
graph[i]에 연결된 노드들의 리스트를 저장
public class AdjacencyList
{
public List<int>[] graph;
public AdjacencyList()
{
graph = new List<int>[8];
for (int i = 0; i < 8; i++)
{
graph[i] = new List<int>();
}
}
public void TwoWayGraph(int a, int b)
{
if (!graph[a].Contains(b)) graph[a].Add(b);
if (!graph[b].Contains(a)) graph[b].Add(a);
}
public void PrintGraph()
{
for (int i = 0; i < graph.Length; i++)
{
Console.Write($"{i}노드 : ");
foreach (var neighbor in graph[i])
{
Console.Write($"- {neighbor} ");
}
Console.WriteLine();
}
}
}