Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14828 was 14277, checked in by gkronber, 8 years ago

#2650: merged r14245:14273 from trunk to branch (fixing conflicts in RegressionSolutionTargetResponseGradientView)

File size: 6.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 System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.IGraph.Wrappers;
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27
28namespace HeuristicLab.Tests {
29  [TestClass]
30  [DeploymentItem("igraph-0.8.0-pre-x86.dll")]
31  [DeploymentItem("igraph-0.8.0-pre-x64.dll")]
32  public class IGraphWrappersGraphTest {
33    [TestMethod]
34    [TestCategory("ExtLibs")]
35    [TestCategory("ExtLibs.igraph")]
36    [TestProperty("Time", "short")]
37    public void IGraphWrappersGraphConstructionAndFinalizationTest() {
38      var graph = new Graph(5, new[] {
39        Tuple.Create(0, 1),
40        Tuple.Create(0, 2),
41        Tuple.Create(1, 2),
42        Tuple.Create(2, 3),
43        Tuple.Create(2, 4),
44        Tuple.Create(3, 4),
45      });
46      Assert.AreEqual(5, graph.Vertices);
47      Assert.IsFalse(graph.IsDirected);
48
49      graph = new Graph(3, new[] {
50        Tuple.Create(0, 1),
51        Tuple.Create(0, 2),
52        Tuple.Create(1, 2),
53      }, directed: true);
54      Assert.AreEqual(3, graph.Vertices);
55      Assert.IsTrue(graph.IsDirected);
56    }
57
58    [TestMethod]
59    [TestCategory("ExtLibs")]
60    [TestCategory("ExtLibs.igraph")]
61    [TestProperty("Time", "short")]
62    public void IGraphWrappersGraphDensityTest() {
63      var graph = new Graph(5, new[] {
64        Tuple.Create(0, 1),
65        Tuple.Create(0, 2),
66        Tuple.Create(1, 2),
67        Tuple.Create(2, 3),
68        Tuple.Create(2, 4),
69        Tuple.Create(3, 4),
70      });
71
72      var density = graph.Density();
73      // in un-directed graphs edges count twice
74      Assert.IsTrue(density.IsAlmost(12 / 20.0));
75
76      graph.Dispose();
77
78      graph = new Graph(5, new[] {
79        Tuple.Create(0, 1),
80        Tuple.Create(0, 2),
81        Tuple.Create(1, 2),
82        Tuple.Create(2, 3),
83        Tuple.Create(2, 4),
84        Tuple.Create(3, 4),
85      }, directed: true);
86
87      density = graph.Density();
88      // in directed graphs edges count twice
89      Assert.IsTrue(density.IsAlmost(6 / 20.0));
90    }
91
92    [TestMethod]
93    [TestCategory("ExtLibs")]
94    [TestCategory("ExtLibs.igraph")]
95    [TestProperty("Time", "short")]
96    public void IGraphWrappersGraphPageRankTest() {
97      var graph = new Graph(4, new[] {
98        Tuple.Create(0, 1),
99        Tuple.Create(0, 2),
100        Tuple.Create(1, 2),
101        Tuple.Create(2, 0),
102        Tuple.Create(3, 2),
103      }, directed: true);
104      var ranks = graph.PageRank();
105      Assert.AreEqual(4, ranks.Length);
106      Assert.AreEqual(0.372, ranks[0], 0.01);
107      Assert.AreEqual(0.195, ranks[1], 0.01);
108      Assert.AreEqual(0.394, ranks[2], 0.01);
109      Assert.AreEqual(0.037, ranks[3], 0.01);
110
111      graph = new Graph(4, new[] {
112        Tuple.Create(0, 1),
113        Tuple.Create(1, 2),
114        Tuple.Create(2, 3),
115        Tuple.Create(3, 0),
116      }, directed: true);
117      ranks = graph.PageRank();
118      Assert.AreEqual(4, ranks.Length);
119      Assert.AreEqual(0.250, ranks[0], 0.01);
120      Assert.AreEqual(0.250, ranks[1], 0.01);
121      Assert.AreEqual(0.250, ranks[2], 0.01);
122      Assert.AreEqual(0.250, ranks[3], 0.01);
123
124      graph = new Graph(4, new[] {
125        Tuple.Create(0, 1),
126        Tuple.Create(0, 2),
127        Tuple.Create(0, 3),
128        Tuple.Create(1, 0),
129        Tuple.Create(2, 0),
130        Tuple.Create(3, 0),
131      }, directed: true);
132      ranks = graph.PageRank();
133      Assert.AreEqual(4, ranks.Length);
134      Assert.AreEqual(0.480, ranks[0], 0.01);
135      Assert.AreEqual(0.173, ranks[1], 0.01);
136      Assert.AreEqual(0.173, ranks[2], 0.01);
137      Assert.AreEqual(0.173, ranks[3], 0.01);
138    }
139
140    [TestMethod]
141    [TestCategory("ExtLibs")]
142    [TestCategory("ExtLibs.igraph")]
143    [TestProperty("Time", "short")]
144    public void IGraphWrappersGraphBreadthFirstWalkTest() {
145      var graph = new Graph(4, new[] {
146        Tuple.Create(0, 1),
147        Tuple.Create(0, 2),
148        Tuple.Create(1, 2),
149        Tuple.Create(2, 0),
150        Tuple.Create(3, 2),
151      }, directed: true);
152      var visited = new HashSet<int>();
153      BreadthFirstHandler handler = (graph1, currentVertexId, previousVertexId, nextVertexId, rank, distance, tag) => {
154        visited.Add(currentVertexId);
155        return false;
156      };
157      graph.BreadthFirstWalk(handler, 0, DirectedWalkMode.All, true, null);
158      Assert.AreEqual(4, visited.Count);
159      Assert.IsTrue(visited.Contains(0));
160      Assert.IsTrue(visited.Contains(1));
161      Assert.IsTrue(visited.Contains(2));
162      Assert.IsTrue(visited.Contains(3));
163    }
164
165    [TestMethod]
166    [TestCategory("ExtLibs")]
167    [TestCategory("ExtLibs.igraph")]
168    [TestProperty("Time", "short")]
169    public void IGraphWrappersGraphDepthFirstWalkTest() {
170      var graph = new Graph(4, new[] {
171        Tuple.Create(0, 1),
172        Tuple.Create(0, 2),
173        Tuple.Create(1, 2),
174        Tuple.Create(2, 0),
175        Tuple.Create(3, 2),
176      }, directed: true);
177      var visited = new HashSet<int>();
178      DepthFirstHandler handler = (graph1, vertexId, distance, tag) => {
179        visited.Add(vertexId);
180        return false;
181      };
182      graph.DepthFirstWalk(handler, handler, 0, DirectedWalkMode.All, true, null);
183      Assert.AreEqual(4, visited.Count);
184      Assert.IsTrue(visited.Contains(0));
185      Assert.IsTrue(visited.Contains(1));
186      Assert.IsTrue(visited.Contains(2));
187      Assert.IsTrue(visited.Contains(3));
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.