source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Wrappers/Graph.cs @ 14244

Last change on this file since 14244 was 14244, checked in by abeham, 3 years ago

#2651: worked on igraph integration, additional layout algorithms, density, page rank

  • added unit tests
File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24
25namespace HeuristicLab.IGraph.Wrappers {
26  public sealed class Graph : IDisposable {
27    private igraph_t graph;
28    internal igraph_t NativeInstance { get { return graph; } }
29
30    public int Vertices { get { return graph.n; } }
31    public bool IsDirected { get { return graph.directed; } }
32
33    public Graph() : this(0) { }
34    public Graph(int vertices) : this(vertices, false) { }
35    public Graph(int vertices, bool directed) {
36      graph = new igraph_t();
37      DllImporter.igraph_empty(graph, vertices, directed);
38    }
39    public Graph(int vertices, IEnumerable<Tuple<int, int>> edges) : this(vertices, edges, false) { }
40    public Graph(int vertices, IEnumerable<Tuple<int, int>> edges, bool directed) {
41      graph = new igraph_t();
42      DllImporter.igraph_empty(graph, vertices, directed);
43      foreach (var e in edges)
44        DllImporter.igraph_add_edge(graph, e.Item1, e.Item2);
45    }
46    ~Graph() {
47      DllImporter.igraph_destroy(graph);
48    }
49
50    public void Dispose() {
51      if (graph == null) return;
52      DllImporter.igraph_destroy(graph);
53      graph = null;
54      GC.SuppressFinalize(this);
55    }
56
57    public void SetSeed(uint seed) {
58      DllImporter.igraph_rng_seed(seed);
59    }
60
61    public int RandomInteger(int inclLower, int inclUpper) {
62      return DllImporter.igraph_rng_get_integer(inclLower, inclUpper);
63    }
64
65    public double Density() {
66      double density;
67      DllImporter.igraph_density(graph, out density, false);
68      return density;
69    }
70
71    public Vector PageRank(double damping = 0.85, Vector weights = null) {
72      var vec = new Vector(Vertices);
73      var all = new igraph_vs_t();
74      DllImporter.igraph_vs_all(ref all);
75      try {
76        double eigenv = 0;
77        DllImporter.igraph_pagerank(graph, igraph_pagerank_algo_t.IGRAPH_PAGERANK_ALGO_PRPACK, vec.NativeInstance, out eigenv, all, IsDirected, damping, weights != null ? weights.NativeInstance : null);
78      } finally {
79        DllImporter.igraph_vs_destroy(ref all);
80      }
81      return vec;
82    }
83
84    public Matrix LayoutWithFruchtermanReingold(Matrix initialCoords = null) {
85      return LayoutWithFruchtermanReingold(500, Math.Sqrt(Vertices), initialCoords);
86    }
87    public Matrix LayoutWithFruchtermanReingold(int niter, double startTemp, Matrix initialCoords = null) {
88      if (initialCoords != null && (initialCoords.Rows != graph.n || initialCoords.Columns != 2))
89        throw new ArgumentException("Initial coordinate matrix does not contain the required number of rows and columns.", "initialCoords");
90      var coords = initialCoords != null ? new Matrix(initialCoords) : new Matrix(graph.n, 2);
91      DllImporter.igraph_layout_fruchterman_reingold(graph, coords.NativeInstance, initialCoords != null, niter, startTemp, igraph_layout_grid_t.IGRAPH_LAYOUT_AUTOGRID, null, null, null, null, null);
92      return coords;
93    }
94
95    public Matrix LayoutWithKamadaKawai(Matrix initialCoords = null) {
96      return LayoutWithKamadaKawai(50 * Vertices, 0, Vertices, initialCoords);
97    }
98    public Matrix LayoutWithKamadaKawai(int maxiter, double epsilon, double kkconst, Matrix initialCoords = null) {
99      if (initialCoords != null && (initialCoords.Rows != graph.n || initialCoords.Columns != 2))
100        throw new ArgumentException("Initial coordinate matrix does not contain the required number of rows and columns.", "initialCoords");
101      var coords = initialCoords != null ? new Matrix(initialCoords) : new Matrix(graph.n, 2);
102      DllImporter.igraph_layout_kamada_kawai(graph, coords.NativeInstance, initialCoords != null, maxiter, epsilon, kkconst, null, null, null, null, null);
103      return coords;
104    }
105
106    public Matrix LayoutWithDavidsonHarel(Matrix initialCoords = null) {
107      var density = Density();
108      return LayoutWithDavidsonHarel(10, Math.Max(10, (int)Math.Log(Vertices, 2)), 0.75, 1.0, 0.0, density / 10.0, 1.0 - Math.Sqrt(density), 0.2 * (1 - density), initialCoords);
109    }
110    public Matrix LayoutWithDavidsonHarel(int maxiter, int fineiter, double cool_fact, double weight_node_dist, double weight_border, double weight_edge_lengths, double weight_edge_crossings, double weight_node_edge_dist, Matrix initialCoords = null) {
111      if (initialCoords != null && (initialCoords.Rows != graph.n || initialCoords.Columns != 2))
112        throw new ArgumentException("Initial coordinate matrix does not contain the required number of rows and columns.", "initialCoords");
113      var coords = initialCoords != null ? new Matrix(initialCoords) : new Matrix(graph.n, 2);
114      DllImporter.igraph_layout_davidson_harel(graph, coords.NativeInstance, initialCoords != null, maxiter, fineiter, cool_fact, weight_node_dist, weight_border, weight_edge_lengths, weight_edge_crossings, weight_node_edge_dist);
115      return coords;
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.