From 6cd486536c4901453e97421657e84698f4d33dd7 Mon Sep 17 00:00:00 2001 From: paramag Date: Wed, 29 Aug 2018 08:15:57 -0700 Subject: [PATCH] graph and stack implementation updates. --- .gitignore | 2 + Algorithm/.vs/Algorithms/v14/.suo | Bin 227840 -> 264704 bytes Algorithm/Algorithms.sln | 19 + .../Algorithms.Problem.InterviewCake.csproj | 2 + Algorithm/InterviewCake/DirtyTestCode.cs | 21 + Algorithm/InterviewCake/RepeatNumbers.cs | 8 +- .../InterviewCake/RepeatNumbersBeastMode.cs | 12 + .../Stacks/Algorithms.Problem.Stacks.csproj | 1 + .../GetMiddleElementConstantTimeProblem.cs | 136 +++++++ Algorithm/Stacks/MinValueInConstantTime.cs | 60 ++- .../DataStructures.Libraries.Graph.csproj | 13 +- DataStructures/Graph/Graph.cs | 371 ++++++++++++++++-- DataStructures/Graph/Graph`1.cs | 92 +++++ .../Graph/{Vertex.cs => Vertex`1.cs} | 0 DataStructures/Graph/graph_example.jpg | Bin 0 -> 57900 bytes 15 files changed, 694 insertions(+), 43 deletions(-) create mode 100644 Algorithm/InterviewCake/DirtyTestCode.cs create mode 100644 Algorithm/InterviewCake/RepeatNumbersBeastMode.cs create mode 100644 Algorithm/Stacks/GetMiddleElementConstantTimeProblem.cs create mode 100644 DataStructures/Graph/Graph`1.cs rename DataStructures/Graph/{Vertex.cs => Vertex`1.cs} (100%) create mode 100644 DataStructures/Graph/graph_example.jpg diff --git a/.gitignore b/.gitignore index 4a17924..370fa46 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,5 @@ /Algorithm/LeetCode/obj/Debug /Algorithm/Stacks/bin/Debug /Algorithm/Stacks/obj/Debug +/Algorithm/InterviewCake/bin/Debug +/Algorithm/InterviewCake/obj/Debug diff --git a/Algorithm/.vs/Algorithms/v14/.suo b/Algorithm/.vs/Algorithms/v14/.suo index 94282bde0edc5005aee2628df6a302cde238f051..4cb26792ebeaac6c7252d01cc5db763ad4cbd8ec 100644 GIT binary patch delta 21350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X|x#NdTt}tsUGF5#u-X$-Tupi5$T|83H`4wNw{oERDbj;4sFLCK32BbNnPN# ZdJWdZrwwWK;g`AT8@p7me4j3}{{gz+0?Ysa diff --git a/Algorithm/Algorithms.sln b/Algorithm/Algorithms.sln index 30d10cf..868f399 100644 --- a/Algorithm/Algorithms.sln +++ b/Algorithm/Algorithms.sln @@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Algorithms.Problems.LeetCod EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Algorithms.Problem.InterviewCake", "InterviewCake\Algorithms.Problem.InterviewCake.csproj", "{9618162B-F6BE-4E79-A256-8B2D34AE7F46}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStructures.Libraries.Graph", "..\DataStructures\Graph\DataStructures.Libraries.Graph.csproj", "{6EB3565B-2658-42A7-80CB-223B6E42013A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -179,11 +181,28 @@ Global {9618162B-F6BE-4E79-A256-8B2D34AE7F46}.Release|x64.Build.0 = Release|Any CPU {9618162B-F6BE-4E79-A256-8B2D34AE7F46}.Release|x86.ActiveCfg = Release|Any CPU {9618162B-F6BE-4E79-A256-8B2D34AE7F46}.Release|x86.Build.0 = Release|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|ARM.ActiveCfg = Debug|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|ARM.Build.0 = Debug|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|x64.ActiveCfg = Debug|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|x64.Build.0 = Debug|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|x86.ActiveCfg = Debug|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|x86.Build.0 = Debug|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|Any CPU.Build.0 = Release|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|ARM.ActiveCfg = Release|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|ARM.Build.0 = Release|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|x64.ActiveCfg = Release|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|x64.Build.0 = Release|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|x86.ActiveCfg = Release|Any CPU + {6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {845E0CA8-AC51-4BAE-95D7-60680CB4A44B} = {B2A3848C-3D95-4B7D-8247-227C7F9A2308} + {6EB3565B-2658-42A7-80CB-223B6E42013A} = {B2A3848C-3D95-4B7D-8247-227C7F9A2308} EndGlobalSection EndGlobal diff --git a/Algorithm/InterviewCake/Algorithms.Problem.InterviewCake.csproj b/Algorithm/InterviewCake/Algorithms.Problem.InterviewCake.csproj index c8f4e66..576d15f 100644 --- a/Algorithm/InterviewCake/Algorithms.Problem.InterviewCake.csproj +++ b/Algorithm/InterviewCake/Algorithms.Problem.InterviewCake.csproj @@ -45,10 +45,12 @@ + + diff --git a/Algorithm/InterviewCake/DirtyTestCode.cs b/Algorithm/InterviewCake/DirtyTestCode.cs new file mode 100644 index 0000000..1325bb8 --- /dev/null +++ b/Algorithm/InterviewCake/DirtyTestCode.cs @@ -0,0 +1,21 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Algorithms.Problem.InterviewCake +{ + [TestClass] + public class DirtyTestCode + { + [TestMethod] + public void TestMethod1() + { + string str = "cats"; + + string subStr = str.Substring(0, str.Length - 1); + } + } +} diff --git a/Algorithm/InterviewCake/RepeatNumbers.cs b/Algorithm/InterviewCake/RepeatNumbers.cs index 5b95b51..17738e2 100644 --- a/Algorithm/InterviewCake/RepeatNumbers.cs +++ b/Algorithm/InterviewCake/RepeatNumbers.cs @@ -57,10 +57,12 @@ public static int FindRepeat(int[] items) public void LongArrayTest() { // var numbers = new int[] { 4, 1, 4, 8, 3, 2, 7, 6, 5 }; - var numbers = new int[] { 5, 8, 4, 1, 3, 2, 7, 6, 8 }; - var expected = 8; + // var numbers = new int[] { 5, 8, 4, 1, 3, 2, 7, 6, 8 }; + + var numbers = new int[] { 1, 3, 9, 8, 4, 6, 1, 2, 5, 7 }; + var expected = 1; var actual = FindRepeat(numbers); Assert.AreEqual(expected, actual); } } -} +} \ No newline at end of file diff --git a/Algorithm/InterviewCake/RepeatNumbersBeastMode.cs b/Algorithm/InterviewCake/RepeatNumbersBeastMode.cs new file mode 100644 index 0000000..b9a0c5a --- /dev/null +++ b/Algorithm/InterviewCake/RepeatNumbersBeastMode.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Algorithms.Problem.InterviewCake +{ + public class RepeatNumbersBeastMode + { + } +} diff --git a/Algorithm/Stacks/Algorithms.Problem.Stacks.csproj b/Algorithm/Stacks/Algorithms.Problem.Stacks.csproj index 8bd20b8..db8f46e 100644 --- a/Algorithm/Stacks/Algorithms.Problem.Stacks.csproj +++ b/Algorithm/Stacks/Algorithms.Problem.Stacks.csproj @@ -44,6 +44,7 @@ + diff --git a/Algorithm/Stacks/GetMiddleElementConstantTimeProblem.cs b/Algorithm/Stacks/GetMiddleElementConstantTimeProblem.cs new file mode 100644 index 0000000..f23528d --- /dev/null +++ b/Algorithm/Stacks/GetMiddleElementConstantTimeProblem.cs @@ -0,0 +1,136 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Algorithms.Problem.Stacks +{ + public class Node + { + public int value; + public Node previous; + public Node next; + + public Node(int value) + { + this.value = value; + } + } + + [TestClass] + public class GetMiddleElementConstantTimeProblem + { + private Node doubleLinkedList; + private Node middleElement; + public int Counter = 0; + + public void Push(int value) + { + Node node = new Node(value); + + if (doubleLinkedList == null) + { + doubleLinkedList = node; + middleElement = doubleLinkedList; + } + else + { + doubleLinkedList.next = node; + doubleLinkedList.next.previous = doubleLinkedList; + + doubleLinkedList = doubleLinkedList.next; + } + + this.Counter += 1; + + if (this.Counter > 1 && !(this.Counter % 2 == 0)) + { + middleElement = middleElement.next; + } + } + + public int Pop() + { + int value = doubleLinkedList.value; + doubleLinkedList = doubleLinkedList.previous; + doubleLinkedList.next = null; + + this.Counter -= 1; + + if (!(this.Counter % 2 == 0)) + { + middleElement = middleElement.previous; + } + + return value; + } + + public int GetMiddleElement() + { + return middleElement.value; + } + + [TestMethod] + public void TestMiddleElementConstantTime() + { + this.Push(1); + this.Push(2); + this.Push(3); + + Assert.AreEqual(2, GetMiddleElement()); + } + + [TestMethod] + public void TestMiddleElementConstantTime2() + { + this.Push(1); + this.Push(2); + this.Push(3); + this.Push(4); + + Assert.AreEqual(2, GetMiddleElement()); + } + + [TestMethod] + public void TestMiddleElementConstantTime3() + { + this.Push(1); + this.Push(2); + this.Push(3); + this.Push(4); + this.Push(5); + + Assert.AreEqual(3, GetMiddleElement()); + } + + [TestMethod] + public void TestMiddleElementConstantTime4() + { + this.Push(1); + this.Push(2); + this.Push(3); + this.Push(4); + this.Push(5); + this.Pop(); + + Assert.AreEqual(3, GetMiddleElement()); + } + + [TestMethod] + public void TestMiddleElementConstantTime5() + { + this.Push(1); + this.Push(2); + this.Push(3); + this.Push(4); + this.Push(5); + + this.Pop(); + this.Pop(); + + Assert.AreEqual(2, GetMiddleElement()); + } + } +} diff --git a/Algorithm/Stacks/MinValueInConstantTime.cs b/Algorithm/Stacks/MinValueInConstantTime.cs index dd84d41..12da099 100644 --- a/Algorithm/Stacks/MinValueInConstantTime.cs +++ b/Algorithm/Stacks/MinValueInConstantTime.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,8 +7,65 @@ namespace Stacks { + /// + /// Get minimum value from stack in constant time. + /// + [TestClass] public class MinValueInConstantTime { + private Stack stack = new Stack(); + private Stack auxillaryStack = new Stack(); + public int GetMinimumValue() + { + return this.auxillaryStack.Peek(); + } + + public void Push(int value) + { + this.stack.Push(value); + + // Use the aux stack (2nd) to push the value if the top of the aux stack is less than the value. + // Also push to aux stack if the stack is empty. + if (this.auxillaryStack.Count == 0 || this.auxillaryStack.Peek() > value) + { + this.auxillaryStack.Push(value); + } + else + { + int auxValue = this.auxillaryStack.Peek(); + this.auxillaryStack.Push(auxValue); + } + } + + public int Pop() + { + int value = this.stack.Pop(); + + // remove from the aux stack if the value is equal to the top of the stack. + if (this.auxillaryStack.Peek() == value) + { + this.auxillaryStack.Pop(); + } + + return value; + } + + [TestMethod] + public void TestGetMinimumInConstantTime() + { + this.Push(10); + this.Push(8); + this.Push(11); + this.Push(5); + this.Push(2); + this.Push(20); + this.Push(17); + this.Push(1); + + int value = this.GetMinimumValue(); + + Assert.AreEqual(value, 1); + } } } diff --git a/DataStructures/Graph/DataStructures.Libraries.Graph.csproj b/DataStructures/Graph/DataStructures.Libraries.Graph.csproj index cb2e1f9..2b28cd6 100644 --- a/DataStructures/Graph/DataStructures.Libraries.Graph.csproj +++ b/DataStructures/Graph/DataStructures.Libraries.Graph.csproj @@ -30,6 +30,10 @@ 4 + + False + ..\..\..\..\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll + @@ -41,9 +45,16 @@ - + + + + + + + +