[8438] | 1 |
|
---|
| 2 | using System;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | namespace HeuristicLab.Problems.RoutePlanning.Graph {
|
---|
[8461] | 5 | public class Graph : IGraph {
|
---|
[8438] | 6 | private IDictionary<long, Vertex> vertices;
|
---|
| 7 | private IDictionary<long, List<Edge<Vertex>>> adjacencyMap;
|
---|
| 8 | private IDictionary<long, List<Edge<Vertex>>> incidenceMap;
|
---|
| 9 |
|
---|
| 10 | public Graph() {
|
---|
| 11 | vertices = new Dictionary<long, Vertex>();
|
---|
| 12 | adjacencyMap = new Dictionary<long, List<Edge<Vertex>>>();
|
---|
[8461] | 13 | incidenceMap = new Dictionary<long, List<Edge<Vertex>>>();
|
---|
[8438] | 14 | }
|
---|
| 15 |
|
---|
| 16 | public void AddVertex(Vertex vertex) {
|
---|
[8461] | 17 | if (!vertices.ContainsKey(vertex.Id)) {
|
---|
| 18 | vertices.Add(vertex.Id, vertex);
|
---|
| 19 | }
|
---|
[8438] | 20 | }
|
---|
| 21 |
|
---|
| 22 | public Vertex GetVertex(long id) {
|
---|
| 23 | Vertex vertex = null;
|
---|
| 24 | vertices.TryGetValue(id, out vertex);
|
---|
| 25 | return vertex;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | public void AddEdge(long startId, long endId) {
|
---|
| 29 | Vertex start = GetVertex(startId);
|
---|
| 30 | if (start == null) throw new ArgumentException(string.Format("Invalid vertex id {0}", startId), "startId");
|
---|
| 31 | Vertex end = GetVertex(startId);
|
---|
| 32 | if (end == null) throw new ArgumentException(string.Format("Invalid vertex id {0}", endId), "endId");
|
---|
| 33 |
|
---|
| 34 | Edge<Vertex> edge = new Edge<Vertex>(start, end);
|
---|
| 35 | //edge.Category = -1
|
---|
| 36 |
|
---|
[8461] | 37 | AddEdge(edge);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public void AddEdge(Edge<Vertex> edge) {
|
---|
| 41 | if (!adjacencyMap.ContainsKey(edge.Source.Id)) {
|
---|
| 42 | adjacencyMap.Add(edge.Source.Id, new List<Edge<Vertex>>());
|
---|
[8438] | 43 | }
|
---|
[8461] | 44 | adjacencyMap[edge.Source.Id].Add(edge);
|
---|
[8438] | 45 |
|
---|
[8461] | 46 | if (!incidenceMap.ContainsKey(edge.Target.Id)) {
|
---|
| 47 | incidenceMap.Add(edge.Target.Id, new List<Edge<Vertex>>());
|
---|
[8438] | 48 | }
|
---|
[8461] | 49 | incidenceMap[edge.Target.Id].Add(edge);
|
---|
[8438] | 50 | }
|
---|
| 51 |
|
---|
[8461] | 52 | public void RemoveEdge(long startId, long endId) {
|
---|
| 53 | Edge<Vertex> edge = GetEdge(startId, endId);
|
---|
| 54 | if (edge != null) {
|
---|
| 55 | adjacencyMap[startId].Remove(edge);
|
---|
| 56 | incidenceMap[endId].Remove(edge);
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public bool HasEdge(long startId, long endId) {
|
---|
| 61 | Edge<Vertex> edge = GetEdge(startId, endId);
|
---|
| 62 | return (edge != null);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[8438] | 65 | public Edge<Vertex> GetEdge(long startId, long endId) {
|
---|
| 66 | List<Edge<Vertex>> edges = adjacencyMap[startId];
|
---|
| 67 | foreach (Edge<Vertex> edge in edges) {
|
---|
| 68 | if (edge.Target.Id == endId) {
|
---|
| 69 | return edge;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | return null;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public List<Edge<Vertex>> GetInEdges(long vertexId) {
|
---|
| 76 | return incidenceMap[vertexId];
|
---|
[8461] | 77 | }
|
---|
[8438] | 78 |
|
---|
[8461] | 79 | public int GetInDegree(long vertexId) {
|
---|
| 80 | return incidenceMap[vertexId].Count;
|
---|
[8438] | 81 | }
|
---|
| 82 |
|
---|
| 83 | public List<Edge<Vertex>> GetOutEdges(long vertexId) {
|
---|
| 84 | return adjacencyMap[vertexId];
|
---|
| 85 | }
|
---|
[8461] | 86 |
|
---|
| 87 | public int GetOutDegree(long vertexId) {
|
---|
| 88 | return adjacencyMap[vertexId].Count;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public List<Edge<Vertex>> GetEdges(long vertexId) {
|
---|
| 92 | List<Edge<Vertex>> result = new List<Edge<Vertex>>();
|
---|
| 93 | foreach (Edge<Vertex> edge in GetInEdges(vertexId)) {
|
---|
| 94 | if (!result.Contains(edge)) {
|
---|
| 95 | result.Add(edge);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | foreach (Edge<Vertex> edge in GetOutEdges(vertexId)) {
|
---|
| 99 | if (!result.Contains(edge)) {
|
---|
| 100 | result.Add(edge);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | return result;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public Dictionary<long, float> GetNeighborsWithWeight(long id) {
|
---|
| 107 | Dictionary<long, float> neighbors = new Dictionary<long, float>();
|
---|
| 108 | List<Edge<Vertex>> edges = GetOutEdges(id);
|
---|
| 109 | foreach (Edge<Vertex> edge in edges) {
|
---|
| 110 | // TODO: check if edge can be traversed (eg. highway tag for car roads, ...)
|
---|
| 111 | float weight = 1; // TODO: edge.Weight?
|
---|
| 112 | neighbors[edge.Target.Id] = weight;
|
---|
| 113 | }
|
---|
| 114 | return neighbors;
|
---|
| 115 | }
|
---|
| 116 | public Dictionary<long, float> GetNeighborsWithWeightReversed(long id) {
|
---|
| 117 | Dictionary<long, float> neighbors = new Dictionary<long, float>();
|
---|
| 118 | List<Edge<Vertex>> edges = GetInEdges(id);
|
---|
| 119 | foreach (Edge<Vertex> edge in edges) {
|
---|
| 120 | // TODO: check if edge can be traversed (eg. highway tag for car roads, ...)
|
---|
| 121 | float weight = 1; // TODO: edge.Weight?
|
---|
| 122 | neighbors[edge.Source.Id] = weight;
|
---|
| 123 | }
|
---|
| 124 | return neighbors;
|
---|
| 125 | }
|
---|
[8438] | 126 | }
|
---|
| 127 | }
|
---|