private static Graph ReadFromFile(string fileName) { //The format of line is: // Id: id_neighbour_1, id_neighbour_2, ... , id_nieghbour_n var g = new Graph(); var allLines = File.ReadAllLines(fileName); var splittedAllLines = allLines.Select(x => x.Split(':')); if (splittedAllLines.Any(x => x.Count() != 2)) throw new InvalidFileFormatException(); foreach (var id in splittedAllLines.Select(x => x[0])) { g.AddVertex(ParseVertexId(id)); } foreach (var line in splittedAllLines) { var fromId = ParseVertexId(line[0]); var neighbours = line[1].Split(','); foreach (var neighbour in neighbours) { int toId = ParseVertexId(neighbour); if(!g.ContainsVertex(toId)) throw new InvalidFileFormatException(); g.AddDirectedEdge(fromId, toId); } } return g; }
public Graph Shrink(int n, int m) { var h = new Graph(); foreach (var v in AllVerticies.Where(x => x.Id != m)) { h.AddVertex(v.Id); } foreach (var vertex in AllVerticies.Where(x => x.Id != m)) { foreach (var neighbour in vertex.AllNeighbours.Where(x => x.Id != m)) { h.AddDirectedEdge(vertex.Id, neighbour.Id); } } var mVertex = verticies[m]; foreach (var neighbourOfM in mVertex.AllNeighbours.Where(x => x.Id != n)) { h.AddUndirectedEdge(n, neighbourOfM.Id); } return h; }