public static int[,] toIncidenceMatrix(Graph g) { int[,] ret = new int[g.Nodes.Count, g.Edges.Count]; for (int i = 0; i < g.Edges.Count; i++) { ret[g.Index(g.Edges[i].From), i] = 1; ret[g.Index(g.Edges[i].To), i] = -1; } return(ret); }
public static int[,] toAdjacencyMatrix(Graph g) { int[,] ret = new int[g.Nodes.Count, g.Nodes.Count]; foreach (Edge e in g.Edges) { int u = g.Index(e.From); int v = g.Index(e.To); ret[u, v] = 1; } return(ret); }
public static int[,] toAdjacencyList(Graph g) { List <List <int> > m = new List <List <int> >(); for (int i = 0; i < g.Nodes.Count; i++) { m.Add(new List <int>()); } Console.WriteLine(m.Count); Console.WriteLine(g.Nodes.Count); foreach (Edge e in g.Edges) { m[g.Index(e.From)].Add(e.To.Id); } int k = 0; for (int i = 0; i < m.Count; i++) { k = Math.Max(k, m[i].Count); } int[,] ret = new int[g.Nodes.Count, k + 1]; for (int i = 0; i < m.Count; i++) { ret[i, 0] = g.Nodes[i].Id; for (int j = 0; j < m[i].Count; j++) { ret[i, j + 1] = m[i][j]; } } return(ret); }
public static int[,] toWeightMatrix(Graph g) { int[,] ret = new int[g.Nodes.Count, g.Nodes.Count]; for (int i = 0; i < ret.GetLength(0); i++) { for (int j = 0; j < ret.GetLength(1); j++) { ret[i, j] = 1000000; } } foreach (Edge e in g.Edges) { int u = g.Index(e.From); int v = g.Index(e.To); ret[u, v] = e.Weight; } return(ret); }