Skip to content

Commit

Permalink
Working algorithm albeit not pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
teemka committed Mar 22, 2017
1 parent 2cced0f commit 2f3d0fe
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 13 deletions.
10 changes: 9 additions & 1 deletion DijkstraOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ namespace Suurballe_s_Algorithm
public List<char> Path { get; set; }
public Dictionary<char, char> Parents { get; set; }
public Dictionary<char, int> Distances { get; set; }
public Dictionary<char,char> DictionaryPath { get; set; }
public DijkstraOut(List<char> _Path, Dictionary<char, char> _Parents, Dictionary<char, int> _Distances)
{
Path = _Path;
Parents = _Parents;
Distances = _Distances;
}
Dictionary<char, char> DictionaryPathTemp = new Dictionary<char, char>();
foreach (var Node in Path)
{
if (Parents.ContainsKey(Node))DictionaryPathTemp[Parents[Node]] = Node;

}
DictionaryPath = DictionaryPathTemp;
}
}

}
85 changes: 78 additions & 7 deletions Graph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void Suurballe(char Start, char Finish)
foreach (var Edge in Vertex.Value.ToList())
{
ResidualGraph.SetEdgeValue(Vertex.Key, Edge.Key, Edge.Value - Dijkstra1.Distances[Edge.Key] + Dijkstra1.Distances[Vertex.Key]);
//replace the cost w(u,v) of every edge (u,v) by w′(u,v) = w(u,v) − d(v) + d(u).
// Replace the cost w(u,v) of every edge (u,v) by w′(u,v) = w(u,v) − d(v) + d(u).
}

}
Expand All @@ -148,18 +148,69 @@ public void Suurballe(char Start, char Finish)
if (Dijkstra1.Parents.TryGetValue(Vertex, out var value))
ResidualGraph.RemoveEdge(Vertex, Dijkstra1.Parents[Vertex]);

//Create a residual graph Gt formed from G by removing the edges of G on path P1 that are directed into start
// Create a residual graph Gt formed from G by removing the edges of G on path P1 that are directed into start
if (Dijkstra1.Parents.TryGetValue(Vertex, out var value1))
ResidualGraph.ReverseEdge(Dijkstra1.Parents[Vertex], Vertex);
//ResidualGraph.AddEdge(Vertex, Dijkstra1.Parents[Vertex], 0);
//reverse the direction of the zero length edges along path P1
}

// ResidualGraph.AddEdge(Vertex, Dijkstra1.Parents[Vertex], 0);
// Reverse the direction of the zero length edges along path P1.
}// Create a residual graph
var Dijkstra2 = ResidualGraph.ShortestPath(Start, Finish);
//Find the shortest path P2 in the residual graph Gt by running Dijkstra's algorithm.
List<KeyValuePair<char, char>> FinalPath1 = new List<KeyValuePair<char, char>>();
List<KeyValuePair<char, char>> FinalPath2 = new List<KeyValuePair<char, char>>();
foreach (var Node1 in Dijkstra1.DictionaryPath.ToList())
{

foreach(var Node2 in Dijkstra2.DictionaryPath.ToList())
{
if(Node1.Key==Node2.Value&&Node1.Value==Node2.Key)
{
Dijkstra1.DictionaryPath.Remove(Node1.Key);
Dijkstra2.DictionaryPath.Remove(Node2.Key);
}

}
}// Discard the common reversed edges between both paths.

FinalPath1.Add(new KeyValuePair<char, char>(Start, Dijkstra1.DictionaryPath[Start]));// Initiate Disjoint Path 1
Dijkstra1.DictionaryPath.Remove(Start);// Add first edge to the path.
FinalPath2.Add(new KeyValuePair<char, char>(Start, Dijkstra2.DictionaryPath[Start]));// Initiate Disjoint Path 2
Dijkstra2.DictionaryPath.Remove(Start);// Add first edge to the path.
while (Dijkstra1.DictionaryPath.ContainsKey(FinalPath1[FinalPath1.Count - 1].Value)
|| Dijkstra2.DictionaryPath.ContainsKey(FinalPath1[FinalPath1.Count - 1].Value))
{
if (Dijkstra1.DictionaryPath.ContainsKey(FinalPath1[FinalPath1.Count - 1].Value))
{
FinalPath1.Add(new KeyValuePair<char, char>(FinalPath1[FinalPath1.Count - 1].Value, Dijkstra1.DictionaryPath[FinalPath1[FinalPath1.Count - 1].Value]));
Dijkstra1.DictionaryPath.Remove(FinalPath1[FinalPath1.Count - 2].Value);
}
if (Dijkstra2.DictionaryPath.ContainsKey(FinalPath1[FinalPath1.Count - 1].Value))
{
FinalPath1.Add(new KeyValuePair<char, char>(FinalPath1[FinalPath1.Count - 1].Value, Dijkstra2.DictionaryPath[FinalPath1[FinalPath1.Count - 1].Value]));
Dijkstra2.DictionaryPath.Remove(FinalPath1[FinalPath1.Count - 2].Value);
}
}// Build Disjoint Path 1 by searching edges outgoing from the vertex at the end of path, while removing edges already added to the Path.
while (Dijkstra1.DictionaryPath.ContainsKey(FinalPath2[FinalPath2.Count - 1].Value)
|| Dijkstra2.DictionaryPath.ContainsKey(FinalPath2[FinalPath2.Count - 1].Value))
{
if (Dijkstra1.DictionaryPath.ContainsKey(FinalPath2[FinalPath2.Count - 1].Value))
{
FinalPath2.Add(new KeyValuePair<char, char>(FinalPath2[FinalPath2.Count - 1].Value, Dijkstra1.DictionaryPath[FinalPath2[FinalPath2.Count - 1].Value]));
Dijkstra1.DictionaryPath.Remove(FinalPath2[FinalPath2.Count - 2].Value);
}
if (Dijkstra2.DictionaryPath.ContainsKey(FinalPath2[FinalPath2.Count - 1].Value))
{
FinalPath2.Add(new KeyValuePair<char, char>(FinalPath2[FinalPath2.Count - 1].Value, Dijkstra2.DictionaryPath[FinalPath2[FinalPath2.Count - 1].Value]));
Dijkstra2.DictionaryPath.Remove(FinalPath2[FinalPath2.Count - 2].Value);
}
}// Build Disjoint Path 2 by searching edges outgoing from the vertex at the end of path, while removing edges already added to the Path.


PrintPath(Dijkstra1.Path);
PrintPath(Dijkstra2.Path);

PrintPathListofKeyValuePair(FinalPath1);
PrintPathListofKeyValuePair(FinalPath2);


}
public void PrintPath(List<char> Path)
Expand All @@ -174,5 +225,25 @@ public void PrintPath(List<char> Path)
}
Console.WriteLine("Distance: {0}", PathDistance);
}
public void PrintDictionaryPath(Dictionary<char, char> DictionaryPath)
{
var PathListofKeyValuePair = DictionaryPath.ToList();
Console.Write(PathListofKeyValuePair[0].Key);
foreach(var Node in PathListofKeyValuePair)
{
Console.Write(" -> " + Node.Value);
}
Console.WriteLine();
}
public void PrintPathListofKeyValuePair(List<KeyValuePair<char, char>> PathListofKeyValuePair)
{

Console.Write(PathListofKeyValuePair[0].Key);
foreach (var Node in PathListofKeyValuePair)
{
Console.Write(" -> " + Node.Value);
}
Console.WriteLine();
}
}
}
1 change: 1 addition & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Buy me a beer
21 changes: 17 additions & 4 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,36 @@ class MainClass
{
public static void Main(string[] args)
{
Graph g = new Graph();
Graph g = new Graph();

//Example from https://en.wikipedia.org/wiki/Suurballe's_algorithm

/*
g.AddVertexAndOutgoingEdges('A', new Dictionary<char, int>() { { 'B', 1 }, { 'C', 2 } });
g.AddVertexAndOutgoingEdges('B', new Dictionary<char, int>() { { 'A', 1 }, { 'D', 1 }, { 'E', 2 } });
g.AddVertexAndOutgoingEdges('C', new Dictionary<char, int>() { { 'A', 2 }, { 'D', 2 } });
g.AddVertexAndOutgoingEdges('D', new Dictionary<char, int>() { { 'C', 2 }, { 'F', 1 }, { 'B', 1 } });
g.AddVertexAndOutgoingEdges('E', new Dictionary<char, int>() { { 'B', 2 }, { 'F', 2 } });
g.AddVertexAndOutgoingEdges('F', new Dictionary<char, int>() { { 'D', 1 }, { 'E', 2 } });
//g.AddVertex('Z');

*/
g.AddVertexAndOutgoingEdges('A', new Dictionary<char, int>() { { 'B', 1 }, { 'C', 3 } });
g.AddVertexAndOutgoingEdges('B', new Dictionary<char, int>() { { 'A', 1 }, { 'D', 1 } });
g.AddVertexAndOutgoingEdges('C', new Dictionary<char, int>() { { 'A', 3 }, { 'E', 3 } });
g.AddVertexAndOutgoingEdges('D', new Dictionary<char, int>() { { 'B', 1 }, { 'E', 1 }, { 'F', 4 } });
g.AddVertexAndOutgoingEdges('E', new Dictionary<char, int>() { { 'C', 3 }, { 'D', 1 }, { 'G', 1 } });
g.AddVertexAndOutgoingEdges('F', new Dictionary<char, int>() { { 'D', 4 }, { 'G', 1 }, { 'H', 1 } });
g.AddVertexAndOutgoingEdges('G', new Dictionary<char, int>() { { 'F', 1 }, { 'E', 1 }, { 'I', 4 } });
g.AddVertexAndOutgoingEdges('H', new Dictionary<char, int>() { { 'F', 1 }, { 'I', 1 }, { 'J', 4 } });
g.AddVertexAndOutgoingEdges('I', new Dictionary<char, int>() { { 'G', 4 }, { 'H', 1 }, { 'J', 1 } });
g.AddVertexAndOutgoingEdges('J', new Dictionary<char, int>() { { 'H', 4 }, { 'I', 1 } });



//g.PrintPath(g.ShortestPath('A', 'F').Path);

g.Suurballe('A', 'F');
//g.Suurballe('A', 'F');
g.Suurballe('A', 'J');

Console.ReadLine();
}
}
Expand Down
Binary file modified bin/Debug/Suurballe's Algorithm.exe
Binary file not shown.
Binary file modified bin/Debug/Suurballe's Algorithm.pdb
Binary file not shown.
2 changes: 1 addition & 1 deletion obj/Debug/CoreCompileInputs.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9fe66ea9177950ef7a4bcbed7c0feff15aaff5d2
56d81845b56cb3aebd247ae9866d311f45d88d33
Binary file modified obj/Debug/Suurballe's Algorithm.exe
Binary file not shown.
Binary file modified obj/Debug/Suurballe's Algorithm.pdb
Binary file not shown.

0 comments on commit 2f3d0fe

Please sign in to comment.