-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cs
115 lines (93 loc) · 2.98 KB
/
Graph.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace Genetic
{
class Graph
{
int numNodes;
int numEdges;
Hashtable nodes;
public int GetNumNodes()
{
return numNodes;
}
public int GetNumEdges()
{
return numEdges;
}
public Hashtable GetNodes()
{
return nodes;
}
public void SetNumNodes(int num)
{
numNodes = num;
}
public void SetNumEdges(int num)
{
numEdges = num;
}
public void SetNodes(Hashtable newNodes)
{
nodes = newNodes;
}
public Graph()
{
nodes = new Hashtable();
numNodes = numEdges = 0;
}
public static Graph LoadFromFile(string filePath)
{
//Le grafo do arquivo de entrada
Graph newGraph = new Graph();
String buffer;
StreamReader file = new StreamReader(filePath);
//Le numero de nodos e arestas
buffer = file.ReadLine();
newGraph.SetNumNodes(Convert.ToInt32(buffer));
buffer = file.ReadLine();
newGraph.SetNumEdges(Convert.ToInt32(buffer));
//Le grau dos vertices
buffer = file.ReadLine();
String[] degrees = buffer.Split(' ');
for (int i = 0; i < degrees.Length; i++)
{
if (degrees[i].Length == 0)
continue;
Node newNode = new Node();
newNode.SetIndex(i);
newNode.SetDegree(Convert.ToInt32(degrees[i]));
newGraph.GetNodes().Add(i, newNode);
}
//Le vizinhos
buffer = file.ReadLine();
String[] edgeSplit = buffer.Split(' ');
int currentNode = 0;
int currentEdge = 0;
for (int i = 0; i < edgeSplit.Length; i += 1)
{
if (edgeSplit[i].Length == 0)
continue;
if (edgeSplit[i].Equals("-1"))
break;
int from = Convert.ToInt32(edgeSplit[i]);
//TODO: revisar representação
Neighbour edge = new Neighbour(from);
if (((Node)newGraph.nodes[currentNode]).GetDegree() == ((Node)newGraph.nodes[currentNode]).GetNeighbours().Count)
{
currentNode++;
currentEdge = 0;
}
((Node)newGraph.nodes[currentNode]).GetNeighbours().Add(currentEdge, edge);
currentEdge++;
}
// ultima linha do arquivo contém os indices dos vértices vizinhos de cada vértices --> não sei se pode complicar mas, ali em cima
// já tá sendo controlado isso.
return newGraph;
}
}
}