Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Wrappers/Graph.cs @ 15130

Last change on this file since 15130 was 15130, checked in by gkronber, 7 years ago

#2651: merged r14234,r14244:14247,r14250,r14256,r14257,r14272 from trunk to stable

File size: 7.5 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 != Vertices || 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(Vertices, 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 != Vertices || 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(Vertices, 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 != Vertices || 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(Vertices, 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    /// <summary>
119    /// Use multi-dimensional scaling to layout vertices.
120    /// A distance matrix can be used to specify the distances between the vertices.
121    /// Otherwise the distances will be calculated by shortest-path-length.
122    /// </summary>
123    /// <remarks>
124    /// For disconnected graphs, dimension must be 2.
125    /// </remarks>
126    /// <param name="dist">The distance matrix to layout the vertices.</param>
127    /// <param name="dim">How many dimensions should be used.</param>
128    /// <returns>The coordinates matrix of the aligned vertices.</returns>
129    public Matrix LayoutWithMds(Matrix dist = null, int dim = 2) {
130      var coords = new Matrix(Vertices, dim);
131      DllImporter.igraph_layout_mds(graph, coords.NativeInstance, dist != null ? dist.NativeInstance : null, dim);
132      return coords;
133    }
134
135    public void BreadthFirstWalk(BreadthFirstHandler handler, int root, DirectedWalkMode mode, bool includeUnreachableFromRoot = false, object tag = null) {
136      igraph_bfshandler_t wrapper = (t, vid, pred, succ, rank, dist, extra) => handler != null && handler(this, vid, pred, succ, rank, dist, tag);
137      DllImporter.igraph_bfs(graph, root, null, (igraph_neimode_t)mode, includeUnreachableFromRoot, null, null, null, null, null, null, null, wrapper, tag);
138    }
139
140    public void DepthFirstWalk(DepthFirstHandler inHandler, DepthFirstHandler outHandler, int root, DirectedWalkMode mode, bool includeUnreachableFromRoot = false, object tag = null) {
141      igraph_dfshandler_t inWrapper = (t, vid, dist, extra) => inHandler != null && inHandler(this, vid, dist, tag);
142      igraph_dfshandler_t outWrapper = (t, vid, dist, extra) => outHandler != null && outHandler(this, vid, dist, tag);
143      DllImporter.igraph_dfs(graph, root, (igraph_neimode_t)mode, includeUnreachableFromRoot, null, null, null, null, inWrapper, outWrapper, tag);
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.