Skip to content

Commit

Permalink
Fixed a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
teemka committed Jun 23, 2017
1 parent 633bac1 commit 2b4332f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
72 changes: 50 additions & 22 deletions Graph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,27 @@ public void SuurballeDisjointVertices(string Start1, string Finish)
{
if (Dijkstra1.Parents.TryGetValue(Vertex, out var value))
{
if (ResidualGraph.Vertices[Vertex].ContainsKey(value))
if (Vertex == value + ".1" ||Vertex+".1"==value)
{
ResidualGraph.RemoveEdge(Vertex, value); // Create a residual graph Gt formed from G by removing the edges of G on path P1 that are directed into start.
continue;
}
try
else
{
var val = GetEdgeValue(value, Vertex);
ResidualGraph.RemoveEdge(value, Vertex);
ResidualGraph.AddEdge(Vertex.Contains(".1") ? Vertex : Vertex + ".1", value.Length == 1 ? value : value.Remove(1), val);
if (ResidualGraph.Vertices[Vertex].ContainsKey(value))
{
ResidualGraph.RemoveEdge(Vertex, value); // Create a residual graph Gt formed from G by removing the edges of G on path P1 that are directed into start.
}
try
{
var val = GetEdgeValue(value, Vertex);
ResidualGraph.RemoveEdge(value, Vertex);
ResidualGraph.AddEdge(Vertex.Contains(".1") ? Vertex : Vertex + ".1", value.Length == 1 ? value : value.Remove(1), val);
}
catch
{ }
// Reverse the direction of the zero length edges along path P1.
}
catch
{ }
// Reverse the direction of the zero length edges along path P1.

}
} // Create a residual graph.

Expand All @@ -285,9 +293,10 @@ public void SuurballeDisjointVertices(string Start1, string Finish)
return;
}
//Find the shortest path P2 in the residual graph Gt by running Dijkstra's algorithm.

List<KeyValuePair<string, string>> FinalPath1 = new List<KeyValuePair<string, string>>();
List<KeyValuePair<string, string>> FinalPath2 = new List<KeyValuePair<string, string>>();
Dijkstra1.EdgePath = MendPath(Dijkstra1.EdgePath);
Dijkstra2.EdgePath = MendPath(Dijkstra2.EdgePath);
Dictionary<string, string> FinalPath1 = new Dictionary<string, string>();
Dictionary<string, string> FinalPath2 = new Dictionary<string, string>();

foreach (var Node1 in Dijkstra1.EdgePath.ToList())
{
Expand All @@ -302,11 +311,11 @@ public void SuurballeDisjointVertices(string Start1, string Finish)
}// Discard the common reversed edges between both paths.
try
{
FinalPath1.Add(new KeyValuePair<string, string>(Start, Dijkstra1.EdgePath[Start]));// Add first edge to the path.
Dijkstra1.EdgePath.Remove(Start);// Shorten the Dictionary
FinalPath1.Add(Start1, Dijkstra1.EdgePath[Start1]);// Add first edge to the path.
Dijkstra1.EdgePath.Remove(Start1);// Shorten the Dictionary

FinalPath2.Add(new KeyValuePair<string, string>(Start, Dijkstra2.EdgePath[Start]));// Add first edge to the path.
Dijkstra2.EdgePath.Remove(Start);// Shorten the Dictionary
FinalPath2.Add(Start1, Dijkstra2.EdgePath[Start1]);// Add first edge to the path.
Dijkstra2.EdgePath.Remove(Start1);// Shorten the Dictionary
}
catch
{
Expand All @@ -320,20 +329,39 @@ public void SuurballeDisjointVertices(string Start1, string Finish)

while(SharedPoolofEdges.ContainsKey(FinalPath1.Last().Value))
{
FinalPath1.Add(new KeyValuePair<string, string>(FinalPath1.Last().Value, SharedPoolofEdges[FinalPath1.Last().Value]));
SharedPoolofEdges.Remove(FinalPath1[FinalPath1.Count - 2].Value);
var last = FinalPath1.Last().Value;
FinalPath1.Add(FinalPath1.Last().Value, SharedPoolofEdges[FinalPath1.Last().Value]);
SharedPoolofEdges.Remove(last);
}// 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 (SharedPoolofEdges.ContainsKey(FinalPath2.Last().Value))
{
FinalPath2.Add(new KeyValuePair<string, string>(FinalPath2.Last().Value, SharedPoolofEdges[FinalPath2.Last().Value]));
SharedPoolofEdges.Remove(FinalPath2[FinalPath2.Count - 2].Value);
var last = FinalPath2.Last().Value;
FinalPath2.Add(FinalPath2.Last().Value, SharedPoolofEdges[FinalPath2.Last().Value]);
SharedPoolofEdges.Remove(last);
}// Build Disjoint Path 2 by searching edges outgoing from the vertex at the end of path, while removing edges already added to the Path.

PrintPathListofKeyValuePairDisjoint(FinalPath1);
PrintPathListofKeyValuePairDisjoint(FinalPath2);
PrintPathListofKeyValuePair(FinalPath1.ToList());
PrintPathListofKeyValuePair(FinalPath2.ToList());

}
private Dictionary<string, string> MendPath(Dictionary<string, string> EdgePath)
{
foreach(KeyValuePair<string,string> edge in EdgePath.ToList())
{
if (edge.Value.EndsWith(".1"))
EdgePath.Remove(edge.Key);
if (edge.Key.EndsWith(".1"))
{
var value = edge.Value;
var key = edge.Key;
key = key.Remove(key.Length - 2);
EdgePath.Remove(edge.Key);
EdgePath.Add(key, value);
}
}
return EdgePath;
}
public void Suurballe(string Start, string Finish)
{
if (!Vertices.ContainsKey(Start))
Expand Down
25 changes: 15 additions & 10 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void Main(string[] args)

for (int i = 65; i < 73; i++)
{
for (int j = 65; j < 73; j++)
for (int j = 70; j < 73; j++)
{
if (j == i) continue;
Graph g = new Graph();
Expand All @@ -45,15 +45,20 @@ public static void Main(string[] args)
//g.AddVertexAndOutgoingEdges("D", new Dictionary<string, int>() { { "A", 1 }, { "C", 1 } });


g.AddVertexAndOutgoingEdges("A", new Dictionary<string, int>() { { "E", 1 }, { "B", 3 } });
g.AddVertexAndOutgoingEdges("B", new Dictionary<string, int>() { { "C", 1 } });
g.AddVertexAndOutgoingEdges("C", new Dictionary<string, int>() { { "H", 1 }, { "E", 1 }, { "D", 1 } });
g.AddVertexAndOutgoingEdges("D", new Dictionary<string, int>() { { "E", 3 }, { "C", 1 }, { "G", 1 } });
g.AddVertexAndOutgoingEdges("E", new Dictionary<string, int>() { { "A", 1 }, { "F", 2 } });
g.AddVertexAndOutgoingEdges("F", new Dictionary<string, int>() { { "G", 3 } });
g.AddVertexAndOutgoingEdges("G", new Dictionary<string, int>() { { "F", 3 }, { "H", 2 } });
g.AddVertexAndOutgoingEdges("H", new Dictionary<string, int>() { { "C", 1 }, { "G", 2 } });

//g.AddVertexAndOutgoingEdges("A", new Dictionary<string, int>() { { "E", 1 }, { "B", 3 } });
//g.AddVertexAndOutgoingEdges("B", new Dictionary<string, int>() { { "C", 1 } });
//g.AddVertexAndOutgoingEdges("C", new Dictionary<string, int>() { { "H", 1 }, { "E", 1 }, { "D", 1 } });
//g.AddVertexAndOutgoingEdges("D", new Dictionary<string, int>() { { "E", 3 }, { "C", 1 }, { "G", 1 } });
//g.AddVertexAndOutgoingEdges("E", new Dictionary<string, int>() { { "A", 1 }, { "F", 2 } });
//g.AddVertexAndOutgoingEdges("F", new Dictionary<string, int>() { { "G", 3 } });
//g.AddVertexAndOutgoingEdges("G", new Dictionary<string, int>() { { "F", 3 }, { "H", 2 } });
//g.AddVertexAndOutgoingEdges("H", new Dictionary<string, int>() { { "C", 1 }, { "G", 2 } });
g.AddVertexAndOutgoingEdges("A", new Dictionary<string, int>() { { "B", 1 }, { "C", 2 } });
g.AddVertexAndOutgoingEdges("B", new Dictionary<string, int>() { { "A", 1 }, { "D", 1 }, { "E", 2 } });
g.AddVertexAndOutgoingEdges("C", new Dictionary<string, int>() { { "A", 2 }, { "D", 2 } });
g.AddVertexAndOutgoingEdges("D", new Dictionary<string, int>() { { "C", 2 }, { "F", 1 }, { "B", 1 } });
g.AddVertexAndOutgoingEdges("E", new Dictionary<string, int>() { { "B", 2 }, { "F", 2 } });
g.AddVertexAndOutgoingEdges("F", new Dictionary<string, int>() { { "D", 1 }, { "E", 2 } });


//Console.Write("Give start vertex:\t");
Expand Down

0 comments on commit 2b4332f

Please sign in to comment.