예제 #1
0
파일: Program.cs 프로젝트: sokolowskip/WMH
        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;
        }
예제 #2
0
파일: Graph.cs 프로젝트: sokolowskip/WMH
 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;
 }