Graphs
Example in Python
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
print(graph)Example in C#
using System;
using System.Collections.Generic;
public class GraphNode {
public string Label;
public List<GraphNode> Adjacent = new List<GraphNode>();
public GraphNode(string label) {
Label = label;
}
public void Connect(GraphNode node) {
Adjacent.Add(node);
}
}
class Program {
static void Main() {
GraphNode a = new GraphNode("A");
GraphNode b = new GraphNode("B");
GraphNode c = new GraphNode("C");
a.Connect(b);
b.Connect(c);
c.Connect(a);
Console.WriteLine($"{a.Label} is connected to {a.Adjacent.Count} nodes.");
}
}Last updated
Was this helpful?