-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdgeWeightedGraph.cs
More file actions
70 lines (60 loc) · 1.7 KB
/
Copy pathEdgeWeightedGraph.cs
File metadata and controls
70 lines (60 loc) · 1.7 KB
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
using System;
using System.Collections.Generic;
namespace UndirectedGraphs
{
/// <summary>
//takes input from file and establishes and EdgeWeighted Graph
/// </summary>
public class EdgeWeightedGraph
{
private int _v;
private int _e;
private LinkedList<Edge>[] _adj;
public EdgeWeightedGraph(int v)
{
_v = v;
_e = 0;
_adj = new LinkedList<Edge>[v];
for(int i=0; i<_v; i++)
{
_adj[i] = new LinkedList<Edge>();
}
}
//Creates Edges from parsed line from input
public EdgeWeightedGraph(string[] input) :this(Convert.ToInt32(input[0]))
{
for(var i =2; i<input.Length; i++)
{
var parsedLine = input[i].Split();
var newEdge = new Edge(Convert.ToInt32(parsedLine[0]), Convert.ToInt32(parsedLine[1]), Convert.ToDouble(parsedLine[2]));
AddEdge(newEdge);
}
}
public int V() => _v;
public int E() => _e;
public void AddEdge(Edge e)
{
int v = e.Either();
int w = e.Other(v);
_adj[v].AddFirst(e);
_adj[w].AddFirst(e);
_e++;
}
public IEnumerable<Edge> Adj(int v)
{
return _adj[v];
}
public IEnumerable<Edge> Edges()
{
var edges = new LinkedList<Edge>();
foreach (var v in _adj)
{
foreach(var e in v)
{
if (e.Either() > e.Other(e.Either())) edges.AddFirst(e);
}
}
return edges;
}
}
}