/// <summary> /// returns graph sample from super graph whith k size /// </summary> /// <param name="super"></param> /// <param name="k"></param> /// <returns></returns> public Graph GetRandomSubgraphESU(Graph super, int k) { if (k <= 2) throw new ArgumentOutOfRangeException(); Graph sub = new Graph(); Random rand = new Random(); int nonNeighborCount = 0; sub.AddVertice(super.Vertices[rand.Next(super.Vertices.Count)]); while (sub.Vertices.Count < k) { Vertice vert = sub.Vertices[rand.Next(sub.Vertices.Count)]; System.Threading.Thread.Sleep(2); List<Vertice> nhood = Graph.GetVerticesNeighborhood(super, sub, vert); if (nhood.Count != 0) { Vertice vertice = nhood[rand.Next(nhood.Count)]; // if (vert.index < vertice.index) // { nonNeighborCount = 0; Edge edge = new Edge(vertice.index, vert.index); sub.AddVertice(vertice); sub.AddEdge(edge); // } // else // { // nonNeighborCount++; // if (nonNeighborCount == 5) // return null; // } } else { nonNeighborCount++; if (nonNeighborCount == 5) return null; } } int verticeIndex = 0; Dictionary<int, int> edges = new Dictionary<int, int>(); foreach (Vertice vertice in sub.Vertices) { edges[vertice.index] = verticeIndex; vertice.index = verticeIndex; verticeIndex++; } sub.changeEdeVerticeIndex(edges); if (sub.Edges.Count == 1) { int edg = sub.Edges.Count; } return sub; }
public static Graph createGraphFromFile() { //Protein-Protein Network Graph graph = new Graph(); for (int i = 0; i < 2114; i++) { graph.AddVertice(new Vertice(i)); } StreamReader reader = new StreamReader("NDYeast.net"); string line = reader.ReadLine(); while (line != null) { if (line[0] == '*') { line = reader.ReadLine(); continue; } string[] tokens = line.Split(' '); int edge1Index = Convert.ToInt32(tokens[0]); for (int i = 1; i < tokens.Length; i++) { graph.AddEdge(new Edge(edge1Index - 1, Convert.ToInt32(tokens[i]) - 1)); } line = reader.ReadLine(); } reader.Close(); return graph; }