Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersGraphTest.cs @ 14245

Last change on this file since 14245 was 14245, checked in by abeham, 8 years ago

#2651: worked on igraph integration

File size: 4.2 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 HeuristicLab.Common;
24using HeuristicLab.IGraph.Wrappers;
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26
27namespace HeuristicLab.Tests {
28  [TestClass]
29  public class IGraphWrappersGraphTest {
30    [TestMethod]
31    [TestCategory("ExtLibs")]
32    [TestCategory("igraph")]
33    [TestProperty("Time", "short")]
34    public void IGraphWrappersGraphConstructionAndFinalization() {
35      var graph = new Graph(5, new[] {
36        Tuple.Create(0, 1),
37        Tuple.Create(0, 2),
38        Tuple.Create(1, 2),
39        Tuple.Create(2, 3),
40        Tuple.Create(2, 4),
41        Tuple.Create(3, 4),
42      });
43      Assert.AreEqual(5, graph.Vertices);
44      Assert.IsFalse(graph.IsDirected);
45
46      graph = new Graph(3, new[] {
47        Tuple.Create(0, 1),
48        Tuple.Create(0, 2),
49        Tuple.Create(1, 2),
50      }, directed: true);
51      Assert.AreEqual(3, graph.Vertices);
52      Assert.IsTrue(graph.IsDirected);
53    }
54
55    [TestMethod]
56    [TestCategory("ExtLibs")]
57    [TestCategory("igraph")]
58    [TestProperty("Time", "short")]
59    public void TestDensity() {
60      var graph = new Graph(5, new[] {
61        Tuple.Create(0, 1),
62        Tuple.Create(0, 2),
63        Tuple.Create(1, 2),
64        Tuple.Create(2, 3),
65        Tuple.Create(2, 4),
66        Tuple.Create(3, 4),
67      });
68
69      var density = graph.Density();
70      // in un-directed graphs edges count twice
71      Assert.IsTrue(density.IsAlmost(12 / 20.0));
72
73      graph.Dispose();
74
75      graph = new Graph(5, new[] {
76        Tuple.Create(0, 1),
77        Tuple.Create(0, 2),
78        Tuple.Create(1, 2),
79        Tuple.Create(2, 3),
80        Tuple.Create(2, 4),
81        Tuple.Create(3, 4),
82      }, directed: true);
83
84      density = graph.Density();
85      // in directed graphs edges count twice
86      Assert.IsTrue(density.IsAlmost(6 / 20.0));
87    }
88
89    [TestMethod]
90    [TestCategory("ExtLibs")]
91    [TestCategory("igraph")]
92    [TestProperty("Time", "short")]
93    public void TestPageRank() {
94      var graph = new Graph(4, new[] {
95        Tuple.Create(0, 1),
96        Tuple.Create(0, 2),
97        Tuple.Create(1, 2),
98        Tuple.Create(2, 0),
99        Tuple.Create(3, 2),
100      }, directed: true);
101      var ranks = graph.PageRank();
102      Assert.AreEqual(4, ranks.Length);
103      Assert.AreEqual(0.372, ranks[0], 0.01);
104      Assert.AreEqual(0.195, ranks[1], 0.01);
105      Assert.AreEqual(0.394, ranks[2], 0.01);
106      Assert.AreEqual(0.037, ranks[3], 0.01);
107
108      graph = new Graph(4, new[] {
109        Tuple.Create(0, 1),
110        Tuple.Create(1, 2),
111        Tuple.Create(2, 3),
112        Tuple.Create(3, 0),
113      }, directed: true);
114      ranks = graph.PageRank();
115      Assert.AreEqual(4, ranks.Length);
116      Assert.AreEqual(0.250, ranks[0], 0.01);
117      Assert.AreEqual(0.250, ranks[1], 0.01);
118      Assert.AreEqual(0.250, ranks[2], 0.01);
119      Assert.AreEqual(0.250, ranks[3], 0.01);
120
121      graph = new Graph(4, new[] {
122        Tuple.Create(0, 1),
123        Tuple.Create(0, 2),
124        Tuple.Create(0, 3),
125        Tuple.Create(1, 0),
126        Tuple.Create(2, 0),
127        Tuple.Create(3, 0),
128      }, directed: true);
129      ranks = graph.PageRank();
130      Assert.AreEqual(4, ranks.Length);
131      Assert.AreEqual(0.480, ranks[0], 0.01);
132      Assert.AreEqual(0.173, ranks[1], 0.01);
133      Assert.AreEqual(0.173, ranks[2], 0.01);
134      Assert.AreEqual(0.173, ranks[3], 0.01);
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.