Beispiel #1
0
 public Graph(Graph g)
 {
     this.AdjacencyMatrix = new Dictionary<int, Dictionary<int, double>>();
     foreach (var ilist in g.AdjacencyMatrix) {
         this.AdjacencyMatrix[ilist.Key] = new Dictionary<int, double>(ilist.Value);
     }
     this.NumEdges = g.NumEdges;
     this.CurrSize = g.CurrSize;
 }
Beispiel #2
0
        static void Main(string[] args)
        {
            Graph g = new Graph();
            //test network
            int edgecounter = 0;
            using (StreamReader sr = new StreamReader("testnetwork.csv"))
            {
                string line = null;
                while ((line = sr.ReadLine()) != null)
                {
                    line=line.Trim();
                    if (line==""){continue;}
                    string[] d = line.Split(',');
                    int agent = Int32.Parse(d[0]);
                    for (int i = 1; i < d.Length; i++)
                    {
                        int friends = Int32.Parse(d[i]);
                        g.AddEdge(agent, friends, 1);
                        edgecounter++;
                    }
                }
            }
            Console.WriteLine("{0} edges added",edgecounter);

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Restart();
            Dictionary<int, int> partition = Community.BestPartition(g);
            Console.WriteLine("BestPartition: {0}", stopwatch.Elapsed);
            var communities = new Dictionary<int, List<int>>();
            foreach (var kvp in partition)
            {
                List<int> nodeset;
                if (!communities.TryGetValue(kvp.Value, out nodeset)) {
                    nodeset = communities[kvp.Value] = new List<int>();
                }
                nodeset.Add(kvp.Key);
            }
            Console.WriteLine("{0} communities found", communities.Count);
            int counter = 0;
            foreach (var kvp in communities)
            {
                Console.WriteLine("community {0}: {1} people",counter,kvp.Value.Count);
                counter++;
            }
            Console.ReadLine();
        }
Beispiel #3
0
 /// <summary>
 /// Creates a 
 /// </summary>
 /// <returns></returns>
 public Graph RandomizedNodes(Random random)
 {
     Graph g = new Graph();
     List<int> nodes = this.Nodes.ToList();
     for (int i = nodes.Count - 1; i >= 1; i--) {
         int j = random.Next(i + 1);
         int v = nodes[i];
         nodes[i] = nodes[j];
         nodes[j] = v;
     }
     Dictionary<int, int> remapping = new Dictionary<int,int>();
     for (int i = 0; i < nodes.Count; i++) {
         remapping[nodes[i]] = i;
     }
     List<Edge> edges = this.Edges.ToList();
     for (int i = edges.Count - 1; i >= 1; i--) {
         int j = random.Next(i + 1);
         Edge v = edges[i];
         edges[i] = edges[j];
         edges[j] = v;
     }
     foreach (var edge in edges) {
         g.AddEdge(remapping[edge.FromNode], remapping[edge.ToNode], edge.Weight);
     }
     return g;
 }
Beispiel #4
0
 /// <summary>
 /// Produces the induced graph from the quotient described by the partition.  The partition is a dictionary from nodes to communities.
 /// The produced graph has nodes which are communities, and there is a link of weight w between communities if the sum of the weights of the links
 /// between their elements is w.
 /// </summary>
 /// <param name="partition">A dictionary where keys are graph nodes and values are the community to which the node belongs.</param>
 /// <returns>The quotient graph.</returns>
 public Graph Quotient(Dictionary<int, int> partition)
 {
     Graph ret = new Graph();
     foreach (int com in partition.Values) {
         ret.AddNode(com);
     }
     foreach (var edge in this.Edges) {
         ret.AddEdge(partition[edge.FromNode], partition[edge.ToNode], edge.Weight);
     }
     return ret;
 }