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

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

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

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