Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Graph/Graph.cs @ 8461

Last change on this file since 8461 was 8461, checked in by spimming, 12 years ago

#1894:

  • Implemented interface IGraph in Graph
  • Equals method in vertex modified
  • Equals method in Edge implemented
  • New algorithm version for IGraph
File size: 4.1 KB
Line 
1
2using System;
3using System.Collections.Generic;
4namespace HeuristicLab.Problems.RoutePlanning.Graph {
5  public class Graph : IGraph {
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>>>();
13      incidenceMap = new Dictionary<long, List<Edge<Vertex>>>();
14    }
15
16    public void AddVertex(Vertex vertex) {
17      if (!vertices.ContainsKey(vertex.Id)) {
18        vertices.Add(vertex.Id, vertex);
19      }
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
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>>());
43      }
44      adjacencyMap[edge.Source.Id].Add(edge);
45
46      if (!incidenceMap.ContainsKey(edge.Target.Id)) {
47        incidenceMap.Add(edge.Target.Id, new List<Edge<Vertex>>());
48      }
49      incidenceMap[edge.Target.Id].Add(edge);
50    }
51
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
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];
77    }
78
79    public int GetInDegree(long vertexId) {
80      return incidenceMap[vertexId].Count;
81    }
82
83    public List<Edge<Vertex>> GetOutEdges(long vertexId) {
84      return adjacencyMap[vertexId];
85    }
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    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.