Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8461


Ignore:
Timestamp:
08/09/12 16:17:37 (12 years ago)
Author:
spimming
Message:

#1894:

  • Implemented interface IGraph in Graph
  • Equals method in vertex modified
  • Equals method in Edge implemented
  • New algorithm version for IGraph
Location:
branches/RoutePlanning
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • branches/RoutePlanning/HeuristicLab.Algorithms.GraphRouting/3.3/HeuristicLab.Algorithms.GraphRouting.csproj

    r8423 r8461  
    6060    <Compile Include="AStarAlgorithmV2.cs" />
    6161    <Compile Include="BidirectionalDijkstraAlgorithm.cs" />
     62    <Compile Include="DijkstraAlgorithmV2.cs" />
    6263    <Compile Include="DijkstraAlgorithm.cs" />
    6364    <Compile Include="GraphRoutingAlgorithm.cs" />
  • branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Graph/Edge.cs

    r8438 r8461  
    3232      return string.Format("{0}->{1}", this.Source, this.Target);
    3333    }
     34
     35    public override bool Equals(object obj) {
     36      if (obj is Edge<Vertex>) {
     37        Edge<Vertex> e = (obj as Edge<Vertex>);
     38        return (this.source.Equals(e.source)) && (this.target.Equals(e.target));
     39      }
     40      return false;
     41    }
     42
     43    public override int GetHashCode() {
     44      return source.GetHashCode() ^ target.GetHashCode();
     45    }
    3446  }
    3547}
  • branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Graph/Graph.cs

    r8438 r8461  
    33using System.Collections.Generic;
    44namespace HeuristicLab.Problems.RoutePlanning.Graph {
    5   public class Graph {
     5  public class Graph : IGraph {
    66    private IDictionary<long, Vertex> vertices;
    77    private IDictionary<long, List<Edge<Vertex>>> adjacencyMap;
     
    1111      vertices = new Dictionary<long, Vertex>();
    1212      adjacencyMap = new Dictionary<long, List<Edge<Vertex>>>();
     13      incidenceMap = new Dictionary<long, List<Edge<Vertex>>>();
    1314    }
    1415
    1516    public void AddVertex(Vertex vertex) {
    16       vertices.Add(vertex.Id, vertex);
     17      if (!vertices.ContainsKey(vertex.Id)) {
     18        vertices.Add(vertex.Id, vertex);
     19      }
    1720    }
    1821
     
    3235      //edge.Category = -1
    3336
    34       if (adjacencyMap[start.Id] == null) {
    35         adjacencyMap[start.Id] = new List<Edge<Vertex>>();
     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>>());
    3643      }
    37       adjacencyMap[start.Id].Add(edge);
     44      adjacencyMap[edge.Source.Id].Add(edge);
    3845
    39       if (incidenceMap[end.Id] == null) {
    40         incidenceMap[end.Id] = new List<Edge<Vertex>>();
     46      if (!incidenceMap.ContainsKey(edge.Target.Id)) {
     47        incidenceMap.Add(edge.Target.Id, new List<Edge<Vertex>>());
    4148      }
    42       incidenceMap[end.Id].Add(edge);
     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);
    4363    }
    4464
     
    5575    public List<Edge<Vertex>> GetInEdges(long vertexId) {
    5676      return incidenceMap[vertexId];
     77    }
    5778
     79    public int GetInDegree(long vertexId) {
     80      return incidenceMap[vertexId].Count;
    5881    }
    5982
     
    6184      return adjacencyMap[vertexId];
    6285    }
     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    }
    63126  }
    64127}
  • branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Graph/IGraph.cs

    r8438 r8461  
    22using System.Collections.Generic;
    33namespace HeuristicLab.Problems.RoutePlanning.Graph {
    4   interface IGraph {
     4  public interface IGraph {
    55    void AddVertex(Vertex vertex);
    66    Vertex GetVertex(long id);
    77    void AddEdge(long startId, long endId);
     8    void AddEdge(Edge<Vertex> edge);
    89    void RemoveEdge(long startId, long endId);
    910    bool HasEdge(long startId, long endId);
  • branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Graph/Vertex.cs

    r8438 r8461  
    5050      if (obj is Vertex) {
    5151        Vertex v = (obj as Vertex);
    52         return this.Equals(v);
     52        return (this.id == v.id); //TODO:
    5353      }
    5454      return false;
Note: See TracChangeset for help on using the changeset viewer.