├── README.md ├── graph ├── AdjacencyListProject │ ├── Demo.cs │ ├── EdgeNode.cs │ ├── LinkedDigraph.cs │ ├── README.md │ └── VertexNode.cs ├── DijkstraShortestPathProject │ ├── Demo.cs │ ├── DirectedWeightedGraph.cs │ ├── README.md │ └── Vertex.cs ├── KruskalsProject │ ├── Demo.cs │ ├── Edge.cs │ ├── PriorityQueue.cs │ ├── QueueNode.cs │ ├── README.md │ ├── UndirectedWeightedGraph.cs │ └── Vertex.cs ├── PathMatrixProject │ ├── Demo.cs │ ├── DirectedGraph.cs │ ├── README.md │ └── Vertex.cs ├── PrimsProject │ ├── Demo.cs │ ├── README.md │ ├── UndirectedWeightedGraph.cs │ └── Vertex.cs ├── README.md ├── WarshallsProject │ ├── Demo.cs │ ├── DirectedGraph.cs │ ├── README.md │ └── Vertex.cs ├── adjacency-matrix │ ├── DirectedGraphProject │ │ ├── Demo.cs │ │ ├── DirectedGraph.cs │ │ └── Vertex.cs │ ├── DirectedWeightedGraphProject │ │ ├── Demo.cs │ │ ├── DirectedWeightedGraph.cs │ │ └── Vertex.cs │ ├── README.md │ ├── UndirectedGraphProject │ │ ├── Demo.cs │ │ ├── UndirectedGraph.cs │ │ └── Vertex.cs │ └── UndirectedWeightedGraphProject │ │ ├── Demo.cs │ │ ├── UndirectedWeightedGraph.cs │ │ └── Vertex.cs ├── breadth-first-search │ ├── BFSConnectedProject │ │ ├── Demo.cs │ │ ├── UndirectedGraph.cs │ │ └── Vertex.cs │ ├── BFSProject │ │ ├── Demo.cs │ │ ├── DirectedGraph.cs │ │ └── Vertex.cs │ ├── BFSShortestPathsProject │ │ ├── Demo.cs │ │ ├── DirectedGraph.cs │ │ └── Vertex.cs │ ├── BFSTreeEdgesProject │ │ ├── Demo.cs │ │ ├── DirectedGraph.cs │ │ └── Vertex.cs │ ├── BFSUndirectedProject │ │ ├── Demo.cs │ │ ├── UndirectedGraph.cs │ │ └── Vertex.cs │ └── README.md └── depth-first-search │ ├── DFSClassifyEdgesProject │ ├── Demo.cs │ ├── DirectedGraph.cs │ └── Vertex.cs │ ├── DFSClassifyEdgesUndirectedProject │ ├── Demo.cs │ ├── UndirectedGraph.cs │ └── Vertex.cs │ ├── DFSProject │ ├── Demo.cs │ ├── DirectedGraph.cs │ └── Vertex.cs │ ├── DFSRecursiveProject │ ├── Demo.cs │ ├── DirectedGraph.cs │ └── Vertex.cs │ ├── DFSRecursiveTimeProject │ ├── Demo.cs │ ├── DirectedGraph.cs │ └── Vertex.cs │ ├── DFSTreeEdgesProject │ ├── Demo.cs │ ├── DirectedGraph.cs │ └── Vertex.cs │ ├── DFSUndirectedProject │ ├── Demo.cs │ ├── UndirectedGraph.cs │ └── Vertex.cs │ └── README.md └── trees ├── AVLTreeProject ├── AVLTree.cs ├── Demo.cs ├── Node.cs └── README.md ├── BtreeProject ├── Btree.cs ├── Node.cs ├── Program.cs └── README.md ├── ExpressionTreeProject ├── Demo.cs ├── ExpressionTree.cs ├── Node.cs ├── README.md └── StackNode.cs ├── README.md └── ThreadedTreeProject ├── Demo.cs ├── Node.cs ├── README.md └── ThreadedBinaryTree.cs /README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C# ( DSA ) 2 | 3 | This [Data Structures and Algorithms in C# (DSA)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-csharp-masterclass?coupon=GITHUB50) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Instructor - Deepali Srivastava, Author of [Ultimate Python Programming](https://www.amazon.in/Ultimate-Python-Programming-programs-questions/dp/935551655X) 7 | * Thoroughly detailed course with complete working programs 8 | * Contains lots of animations to help you visualize the concepts 9 | * Includes AVL tree, BTree, Graph Algorithms 10 | * Builds a solid foundation in Data Structures and Algorithms 11 | * Prepares you for coding interviews 12 | * Lifetime Access 13 | 14 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 15 | 16 | [![data-structures-algorithms-csharp](https://user-images.githubusercontent.com/96913690/200234905-67b85dfd-20c4-4f4b-afd2-e10d3568fff8.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-csharp-masterclass?coupon=GITHUB50) 17 | [![data-structures-algorithms-python](https://user-images.githubusercontent.com/96913690/200234827-86aec10a-bfab-4371-91fc-e2be855ff1ff.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-python-masterclass?coupon=GITHUB50) 18 | [![data-structures-algorithms-java](https://user-images.githubusercontent.com/96913690/200234744-14a5ed97-085f-44f3-9298-979c2053c580.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) 19 | [![data-structures-algorithms-c](https://user-images.githubusercontent.com/96913690/200234592-25d33957-0e9e-4cc0-b324-2a73325aca85.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-c-masterclass?coupon=GITHUB50) 20 | 21 | 29 | 30 | ## Copyright 31 | © Copyright Deepali Srivastava : All rights reserved. 32 | Not to be used for commercial purposes. 33 | -------------------------------------------------------------------------------- /graph/AdjacencyListProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace AdjacencyListProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | LinkedDigraph g = new LinkedDigraph(); 15 | 16 | int choice; 17 | String s1, s2; 18 | 19 | while(true) 20 | { 21 | Console.WriteLine("1.Display "); 22 | Console.WriteLine("2.Insert a vertex"); 23 | Console.WriteLine("3.Delete a vertex"); 24 | Console.WriteLine("4.Insert an edge"); 25 | Console.WriteLine("5.Delete an edge"); 26 | Console.WriteLine("6.Display Indegree and outdegree of a vertex"); 27 | Console.WriteLine("7.Check if there is an edge between two vertices"); 28 | Console.WriteLine("8.Exit"); 29 | Console.Write("Enter your choice : "); 30 | choice = Convert.ToInt32(Console.ReadLine()); 31 | if(choice == 8) 32 | break; 33 | 34 | switch(choice) 35 | { 36 | case 1: 37 | Console.WriteLine(); 38 | g.Display(); 39 | Console.WriteLine("Vertices = " + g.Vertices() ); 40 | Console.WriteLine("Edges = " + g.Edges()); 41 | break; 42 | case 2: 43 | Console.Write("Enter a vertex to be inserted : "); 44 | s1 = Console.ReadLine(); 45 | g.InsertVertex(s1); 46 | break; 47 | case 3: 48 | Console.Write("Enter a vertex to be deleted : "); 49 | s1 = Console.ReadLine(); 50 | g.DeleteVertex(s1); 51 | break; 52 | case 4: 53 | Console.Write("Enter an edge to be inserted : "); 54 | s1 = Console.ReadLine(); 55 | s2 = Console.ReadLine(); 56 | g.InsertEdge(s1,s2); 57 | break; 58 | case 5: 59 | Console.Write("Enter an edge to be deleted : "); 60 | s1 = Console.ReadLine(); 61 | s2 = Console.ReadLine(); 62 | g.DeleteEdge(s1,s2); 63 | break; 64 | case 6: 65 | Console.Write("Enter a vertex : "); 66 | s1 = Console.ReadLine(); 67 | Console.WriteLine("Indegree is : " + g.inDegree(s1)); 68 | Console.WriteLine("Outdegree is : " + g.outDegree(s1)); 69 | break; 70 | case 7: 71 | Console.Write("Enter two vertices : "); 72 | s1 = Console.ReadLine(); 73 | s2 = Console.ReadLine(); 74 | if(g.EdgeExists(s1,s2)) 75 | Console.WriteLine("Vertex " + s2 + " is adjacent to vertex " + s1); 76 | else 77 | Console.WriteLine("Vertex " + s2 + " is not adjacent to vertex " + s1); 78 | 79 | if (g.EdgeExists(s2,s1)) 80 | Console.WriteLine("Vertex " + s1 + " is adjacent to vertex " + s2); 81 | else 82 | Console.WriteLine("Vertex " + s1 + " is not adjacent to vertex " + s2); 83 | break; 84 | default: 85 | Console.WriteLine("Wrong choice"); 86 | break; 87 | } 88 | } 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /graph/AdjacencyListProject/EdgeNode.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace AdjacencyListProject 9 | { 10 | class EdgeNode 11 | { 12 | public VertexNode endVertex; 13 | public EdgeNode nextEdge; 14 | 15 | public EdgeNode(VertexNode v) 16 | { 17 | endVertex = v; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /graph/AdjacencyListProject/LinkedDigraph.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace AdjacencyListProject 9 | { 10 | class LinkedDigraph 11 | { 12 | VertexNode start; 13 | int n; 14 | int e; 15 | 16 | public int Vertices() 17 | { 18 | return n; 19 | } 20 | 21 | public int Edges() 22 | { 23 | return e; 24 | } 25 | 26 | public void InsertVertex(String s) 27 | { 28 | VertexNode temp = new VertexNode(s); 29 | 30 | if (start == null) 31 | start = temp; 32 | else 33 | { 34 | VertexNode p = start; 35 | while (p.nextVertex != null) 36 | { 37 | if(p.name.Equals(s)) 38 | { 39 | Console.WriteLine("Vertex already present"); 40 | return; 41 | } 42 | p = p.nextVertex; 43 | } 44 | if(p.name.Equals(s)) 45 | { 46 | Console.WriteLine("Vertex already present"); 47 | return; 48 | } 49 | p.nextVertex = temp; 50 | } 51 | n++; 52 | } 53 | 54 | public void DeleteVertex(String s) 55 | { 56 | DeletefromEdgeLists(s); 57 | DeletefromVertexList(s); 58 | } 59 | 60 | /*Delete incoming edges*/ 61 | private void DeletefromEdgeLists(String s) 62 | { 63 | for (VertexNode p = start; p != null; p = p.nextVertex) 64 | { 65 | if (p.firstEdge == null) 66 | continue; 67 | 68 | if (p.firstEdge.endVertex.name.Equals(s)) 69 | { 70 | p.firstEdge = p.firstEdge.nextEdge; 71 | e--; 72 | } 73 | else 74 | { 75 | EdgeNode q = p.firstEdge; 76 | while (q.nextEdge != null) 77 | { 78 | if (q.nextEdge.endVertex.name.Equals(s)) 79 | { 80 | q.nextEdge = q.nextEdge.nextEdge; 81 | e--; 82 | break; 83 | } 84 | q = q.nextEdge; 85 | } 86 | } 87 | } 88 | } 89 | 90 | 91 | private void DeletefromVertexList(String s) 92 | { 93 | if (start == null) 94 | { 95 | Console.WriteLine("No vertices to be deleted"); 96 | return; 97 | } 98 | 99 | if (start.name.Equals(s)) /* Vertex to be deleted is first vertex of list*/ 100 | { 101 | for(EdgeNode q = start.firstEdge; q != null; q = q.nextEdge) 102 | e--; 103 | start = start.nextVertex; 104 | n--; 105 | } 106 | else 107 | { 108 | VertexNode p = start; 109 | while (p.nextVertex != null) 110 | { 111 | if(p.nextVertex.name.Equals(s)) 112 | break; 113 | p = p.nextVertex; 114 | } 115 | 116 | if (p.nextVertex == null) 117 | { 118 | Console.WriteLine("Vertex not found"); 119 | return; 120 | } 121 | else 122 | { 123 | for (EdgeNode q = p.nextVertex.firstEdge; q != null; q = q.nextEdge) 124 | e--; 125 | p.nextVertex = p.nextVertex.nextVertex; 126 | n--; 127 | } 128 | } 129 | } 130 | 131 | private VertexNode FindVertex(String s) 132 | { 133 | VertexNode p = start; 134 | while (p != null) 135 | { 136 | if (p.name.Equals(s)) 137 | return p; 138 | p = p.nextVertex; 139 | } 140 | return null; 141 | } 142 | 143 | /*Insert an edge (s1,s2) */ 144 | public void InsertEdge(String s1, String s2) 145 | { 146 | if (s1.Equals(s2)) 147 | { 148 | Console.WriteLine("Inavlid Edge : Start and end vertices are same"); 149 | return; 150 | } 151 | 152 | VertexNode u = FindVertex(s1); 153 | VertexNode v = FindVertex(s2); 154 | 155 | if (u == null) 156 | { 157 | Console.WriteLine("Start vertex not present, first insert vertex " + s1); 158 | return; 159 | } 160 | if (v == null) 161 | { 162 | Console.WriteLine("End vertex not present, first insert vertex " + s2); 163 | return; 164 | } 165 | 166 | EdgeNode temp = new EdgeNode(v); 167 | if (u.firstEdge == null) 168 | { 169 | u.firstEdge = temp; 170 | e++; 171 | } 172 | else 173 | { 174 | EdgeNode p = u.firstEdge; 175 | while (p.nextEdge != null) 176 | { 177 | if (p.endVertex.name.Equals(s2)) 178 | { 179 | Console.WriteLine("Edge present"); 180 | return; 181 | } 182 | p = p.nextEdge; 183 | } 184 | if (p.endVertex.name.Equals(s2)) 185 | { 186 | Console.WriteLine("Edge present"); 187 | return; 188 | } 189 | p.nextEdge = temp; 190 | e++; 191 | } 192 | } 193 | 194 | /* Delete the edge (s1,s2) */ 195 | public void DeleteEdge(String s1, String s2) 196 | { 197 | VertexNode u = FindVertex(s1); 198 | 199 | if (u == null) 200 | { 201 | Console.WriteLine("Start vertex not present"); 202 | return; 203 | } 204 | if (u.firstEdge == null) 205 | { 206 | Console.WriteLine("Edge not present"); 207 | return; 208 | } 209 | 210 | if (u.firstEdge.endVertex.name.Equals(s2)) 211 | { 212 | u.firstEdge = u.firstEdge.nextEdge; 213 | e--; 214 | return; 215 | } 216 | EdgeNode q = u.firstEdge; 217 | while (q.nextEdge != null) 218 | { 219 | if (q.nextEdge.endVertex.name.Equals(s2)) 220 | { 221 | q.nextEdge = q.nextEdge.nextEdge; 222 | e--; 223 | return; 224 | } 225 | q = q.nextEdge; 226 | } 227 | Console.WriteLine("Edge not present"); 228 | } 229 | 230 | public void Display() 231 | { 232 | EdgeNode q; 233 | for (VertexNode p = start; p != null; p = p.nextVertex) 234 | { 235 | Console.Write(p.name + "->"); 236 | for (q = p.firstEdge; q != null; q = q.nextEdge) 237 | Console.Write(" " + q.endVertex.name); 238 | Console.WriteLine(); 239 | } 240 | } 241 | 242 | /* Returns true if s2 is adjacent to s1, i.e. if edge (s1,s2) exists */ 243 | public bool EdgeExists(String s1, String s2) 244 | { 245 | VertexNode u = FindVertex(s1); 246 | EdgeNode q = u.firstEdge; 247 | while (q != null) 248 | { 249 | if (q.endVertex.name.Equals(s2)) 250 | return true; 251 | q = q.nextEdge; 252 | } 253 | return false; 254 | } 255 | 256 | /*Returns number of edges going out from vertex s*/ 257 | public int outDegree(String s) 258 | { 259 | VertexNode u = FindVertex(s); 260 | if (u == null) 261 | throw new System.InvalidOperationException("Vertex not present"); 262 | 263 | int outd = 0; 264 | EdgeNode q = u.firstEdge; 265 | while (q != null) 266 | { 267 | q = q.nextEdge; 268 | outd++; 269 | } 270 | return outd; 271 | } 272 | 273 | /*Returns number of edges coming to vertex s*/ 274 | public int inDegree(String s) 275 | { 276 | VertexNode u = FindVertex(s); 277 | if (u == null) 278 | throw new System.InvalidOperationException("Vertex not present"); 279 | 280 | int ind = 0; 281 | for (VertexNode p = start; p != null; p = p.nextVertex) 282 | { 283 | for (EdgeNode q = p.firstEdge; q != null; q = q.nextEdge) 284 | if ( q.endVertex.name.Equals(s) ) 285 | ind++; 286 | } 287 | return ind; 288 | } 289 | } 290 | } 291 | -------------------------------------------------------------------------------- /graph/AdjacencyListProject/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C# 2 | 3 | This [“Advanced Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes AVL tree, BTree, Graph Algorithms 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 20 | [![data-structures- and-algorithms-in-python-2](https://user-images.githubusercontent.com/98641125/153196106-0eb1a386-c36b-4f14-8675-9d865438f882.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-python-2/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /graph/AdjacencyListProject/VertexNode.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace AdjacencyListProject 9 | { 10 | class VertexNode 11 | { 12 | public String name; 13 | public VertexNode nextVertex; 14 | public EdgeNode firstEdge; 15 | 16 | public VertexNode(String s) 17 | { 18 | name = s; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /graph/DijkstraShortestPathProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace DijkstraShortestPathProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | DirectedWeightedGraph g = new DirectedWeightedGraph(); 15 | 16 | g.InsertVertex("Zero"); 17 | g.InsertVertex("One"); 18 | g.InsertVertex("Two"); 19 | g.InsertVertex("Three"); 20 | g.InsertVertex("Four"); 21 | g.InsertVertex("Five"); 22 | g.InsertVertex("Six"); 23 | g.InsertVertex("Seven"); 24 | g.InsertVertex("Eight"); 25 | 26 | g.InsertEdge("Zero", "Three", 2); 27 | g.InsertEdge("Zero", "One", 5); 28 | g.InsertEdge("Zero", "Four", 8); 29 | g.InsertEdge("One", "Four", 2); 30 | g.InsertEdge("Two", "One", 3); 31 | g.InsertEdge("Two", "Five", 4); 32 | g.InsertEdge("Three", "Four", 7); 33 | g.InsertEdge("Three", "Six", 8); 34 | g.InsertEdge("Four", "Five", 9); 35 | g.InsertEdge("Four", "Seven", 4); 36 | g.InsertEdge("Five", "One", 6); 37 | g.InsertEdge("Six", "Seven", 9); 38 | g.InsertEdge("Seven", "Three", 5); 39 | g.InsertEdge("Seven", "Five", 3); 40 | g.InsertEdge("Seven", "Eight", 5); 41 | g.InsertEdge("Eight", "Five", 3); 42 | 43 | g.FindPaths("Zero"); 44 | 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /graph/DijkstraShortestPathProject/DirectedWeightedGraph.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace DijkstraShortestPathProject 9 | { 10 | class DirectedWeightedGraph 11 | { 12 | public readonly int MAX_VERTICES = 30; 13 | 14 | int n; 15 | int e; 16 | int[,] adj; 17 | Vertex[] vertexList; 18 | 19 | private readonly int TEMPORARY = 1; 20 | private readonly int PERMANENT = 2; 21 | private readonly int NIL = -1; 22 | private readonly int INFINITY = 99999; 23 | 24 | public DirectedWeightedGraph() 25 | { 26 | adj = new int[MAX_VERTICES, MAX_VERTICES]; 27 | vertexList = new Vertex[MAX_VERTICES]; 28 | } 29 | 30 | 31 | private void Dijkstra(int s) 32 | { 33 | int v, c; 34 | 35 | for (v = 0; v < n; v++) 36 | { 37 | vertexList[v].status = TEMPORARY; 38 | vertexList[v].pathLength = INFINITY; 39 | vertexList[v].predecessor = NIL; 40 | } 41 | 42 | vertexList[s].pathLength = 0; 43 | 44 | while (true) 45 | { 46 | c = TempVertexMinPL(); 47 | 48 | if (c == NIL) 49 | return; 50 | 51 | vertexList[c].status = PERMANENT; 52 | 53 | for (v = 0; v < n; v++) 54 | { 55 | if (IsAdjacent(c, v) && vertexList[v].status == TEMPORARY) 56 | if (vertexList[c].pathLength + adj[c,v] < vertexList[v].pathLength) 57 | { 58 | vertexList[v].predecessor = c; 59 | vertexList[v].pathLength = vertexList[c].pathLength + adj[c,v]; 60 | } 61 | } 62 | } 63 | } 64 | 65 | private int TempVertexMinPL() 66 | { 67 | int min = INFINITY; 68 | int x = NIL; 69 | for (int v = 0; v < n; v++) 70 | { 71 | if (vertexList[v].status == TEMPORARY && vertexList[v].pathLength < min) 72 | { 73 | min = vertexList[v].pathLength; 74 | x = v; 75 | } 76 | } 77 | return x; 78 | } 79 | 80 | public void FindPaths(String source) 81 | { 82 | int s = GetIndex(source); 83 | 84 | Dijkstra(s); 85 | 86 | Console.WriteLine("Source Vertex : " + source + "\n"); 87 | 88 | for (int v = 0; v < n; v++) 89 | { 90 | Console.WriteLine("Destination Vertex : " + vertexList[v].name); 91 | if ( vertexList[v].pathLength == INFINITY ) 92 | Console.WriteLine("There is no path from " + source + " to vertex " + vertexList[v].name + "\n"); 93 | else 94 | FindPath(s,v); 95 | } 96 | } 97 | 98 | private void FindPath(int s, int v) 99 | { 100 | int i, u; 101 | int [] path = new int[n]; 102 | int sd = 0; 103 | int count = 0; 104 | 105 | while (v != s) 106 | { 107 | count++; 108 | path[count] = v; 109 | u = vertexList[v].predecessor; 110 | sd += adj[u,v]; 111 | v=u; 112 | } 113 | count++; 114 | path[count] = s; 115 | 116 | Console.Write("Shortest Path is : "); 117 | for (i = count; i>=1; i--) 118 | Console.Write(path[i] + " "); 119 | Console.WriteLine("\n Shortest distance is : " + sd + "\n"); 120 | } 121 | 122 | private int GetIndex(String s) 123 | { 124 | for (int i = 0; i < n; i++) 125 | if (s.Equals(vertexList[i].name)) 126 | return i; 127 | throw new System.InvalidOperationException("Invalid Vertex"); 128 | } 129 | 130 | public void InsertVertex(String name) 131 | { 132 | vertexList[n++] = new Vertex(name); 133 | } 134 | 135 | 136 | private bool IsAdjacent(int u, int v) 137 | { 138 | return (adj[u, v] != 0); 139 | } 140 | 141 | /*Insert an edge (s1,s2) */ 142 | public void InsertEdge(String s1, String s2, int wt) 143 | { 144 | int u = GetIndex(s1); 145 | int v = GetIndex(s2); 146 | if (u == v) 147 | throw new System.InvalidOperationException("Not a valid edge"); 148 | 149 | if (adj[u, v] != 0) 150 | Console.Write("Edge already present"); 151 | else 152 | { 153 | adj[u, v] = wt; 154 | e++; 155 | } 156 | } 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /graph/DijkstraShortestPathProject/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C# 2 | 3 | This [“Advanced Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes AVL tree, BTree, Graph Algorithms 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 20 | [![data-structures- and-algorithms-in-python-2](https://user-images.githubusercontent.com/98641125/153196106-0eb1a386-c36b-4f14-8675-9d865438f882.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-python-2/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /graph/DijkstraShortestPathProject/Vertex.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace DijkstraShortestPathProject 9 | { 10 | class Vertex 11 | { 12 | public String name; 13 | public int status; 14 | public int predecessor; 15 | public int pathLength; 16 | 17 | public Vertex(String name) 18 | { 19 | this.name = name; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /graph/KruskalsProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace KruskalsProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | UndirectedWeightedGraph g = new UndirectedWeightedGraph(); 15 | 16 | g.InsertVertex("Zero"); 17 | g.InsertVertex("One"); 18 | g.InsertVertex("Two"); 19 | g.InsertVertex("Three"); 20 | g.InsertVertex("Four"); 21 | g.InsertVertex("Five"); 22 | g.InsertVertex("Six"); 23 | g.InsertVertex("Seven"); 24 | g.InsertVertex("Eight"); 25 | g.InsertVertex("Nine"); 26 | 27 | g.InsertEdge("Zero", "One", 19); 28 | g.InsertEdge("Zero", "Three", 14); 29 | g.InsertEdge("Zero", "Four", 12); 30 | g.InsertEdge("One", "Two", 20); 31 | g.InsertEdge("One", "Four", 18); 32 | g.InsertEdge("Two", "Four", 17); 33 | g.InsertEdge("Two", "Five", 15); 34 | g.InsertEdge("Two", "Nine", 29); 35 | g.InsertEdge("Three", "Four", 13); 36 | g.InsertEdge("Three", "Six", 28); 37 | g.InsertEdge("Four", "Five", 16); 38 | g.InsertEdge("Four", "Six", 21); 39 | g.InsertEdge("Four", "Seven", 22); 40 | g.InsertEdge("Four", "Eight", 24); 41 | g.InsertEdge("Five", "Eight", 26); 42 | g.InsertEdge("Five", "Nine", 27); 43 | g.InsertEdge("Six", "Seven", 23); 44 | g.InsertEdge("Seven", "Eight", 30); 45 | g.InsertEdge("Eight", "Nine", 35); 46 | 47 | g.Kruskals(); 48 | 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /graph/KruskalsProject/Edge.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace KruskalsProject 9 | { 10 | class Edge 11 | { 12 | public int u; 13 | public int v; 14 | public int wt; 15 | 16 | public Edge(int u, int v, int wt) 17 | { 18 | this.u = u; 19 | this.v = v; 20 | this.wt = wt; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /graph/KruskalsProject/PriorityQueue.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace KruskalsProject 9 | { 10 | class PriorityQueue 11 | { 12 | private QueueNode front; 13 | 14 | public PriorityQueue() 15 | { 16 | front=null; 17 | } 18 | 19 | public void Insert(Edge e) 20 | { 21 | QueueNode temp,p; 22 | 23 | temp = new QueueNode(e); 24 | 25 | /*Queue is empty or element to be added has priority more than first element*/ 26 | if( IsEmpty() || e.wt < front.info.wt ) 27 | { 28 | temp.link=front; 29 | front=temp; 30 | } 31 | else 32 | { 33 | p=front; 34 | while( p.link!=null && p.link.info.wt <= e.wt ) 35 | p=p.link; 36 | temp.link=p.link; 37 | p.link=temp; 38 | } 39 | } 40 | 41 | public Edge Delete() 42 | { 43 | Edge e; 44 | if(IsEmpty()) 45 | throw new InvalidOperationException("Queue Underflow"); 46 | else 47 | { 48 | e=front.info; 49 | front=front.link; 50 | } 51 | return e; 52 | } 53 | 54 | public bool IsEmpty() 55 | { 56 | return (front==null); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /graph/KruskalsProject/QueueNode.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace KruskalsProject 9 | { 10 | class QueueNode 11 | { 12 | public Edge info; 13 | public QueueNode link; 14 | 15 | public QueueNode(Edge e) 16 | { 17 | info=e; 18 | link=null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /graph/KruskalsProject/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C# 2 | 3 | This [“Advanced Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes AVL tree, BTree, Graph Algorithms 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 20 | [![data-structures- and-algorithms-in-python-2](https://user-images.githubusercontent.com/98641125/153196106-0eb1a386-c36b-4f14-8675-9d865438f882.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-python-2/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /graph/KruskalsProject/UndirectedWeightedGraph.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace KruskalsProject 9 | { 10 | class UndirectedWeightedGraph 11 | { 12 | public readonly int MAX_VERTICES = 30; 13 | 14 | int n; 15 | int e; 16 | int[,] adj; 17 | Vertex[] vertexList; 18 | 19 | private readonly int NIL = -1; 20 | 21 | public UndirectedWeightedGraph() 22 | { 23 | adj = new int[MAX_VERTICES, MAX_VERTICES]; 24 | vertexList = new Vertex[MAX_VERTICES]; 25 | } 26 | 27 | public int Vertices() 28 | { 29 | return n; 30 | } 31 | 32 | public int Edges() 33 | { 34 | return e; 35 | } 36 | 37 | public void Kruskals() 38 | { 39 | PriorityQueue pq = new PriorityQueue(); 40 | 41 | int u,v; 42 | for (u = 0; u < n; u++) 43 | for (v = 0; v < u; v++) 44 | { 45 | if (adj[u,v] != 0) 46 | pq.Insert(new Edge(u,v,adj[u,v])); 47 | } 48 | 49 | for (v = 0; v < n; v++) 50 | vertexList[v].father = NIL; 51 | 52 | int v1, v2, r1 = NIL, r2 = NIL; 53 | int edgesInTree = 0; 54 | int wtTree = 0; 55 | 56 | while (!pq.IsEmpty() && edgesInTree " + vertexList[v2].name ); 76 | wtTree += edge.wt; 77 | vertexList[r2].father = r1; 78 | } 79 | } 80 | 81 | if(edgesInTree < n-1) 82 | throw new InvalidOperationException 83 | ("Graph is not connected, no spanning tree possible\n"); 84 | 85 | Console.WriteLine("Weight of Minimum Spanning Tree is " + wtTree); 86 | } 87 | 88 | 89 | private int GetIndex(String s) 90 | { 91 | for (int i = 0; i < n; i++) 92 | if (s.Equals(vertexList[i].name)) 93 | return i; 94 | throw new System.InvalidOperationException("Invalid Vertex"); 95 | } 96 | 97 | public void InsertVertex(String name) 98 | { 99 | vertexList[n++] = new Vertex(name); 100 | } 101 | 102 | 103 | private bool IsAdjacent(int u, int v) 104 | { 105 | return (adj[u, v] != 0); 106 | } 107 | 108 | /*Insert an edge (s1,s2) */ 109 | public void InsertEdge(String s1, String s2, int wt) 110 | { 111 | int u = GetIndex(s1); 112 | int v = GetIndex(s2); 113 | if (adj[u, v] != 0) 114 | Console.Write("Edge already present"); 115 | else 116 | { 117 | adj[u, v] = wt; 118 | adj[v, u] = wt; 119 | e++; 120 | } 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /graph/KruskalsProject/Vertex.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace KruskalsProject 9 | { 10 | class Vertex 11 | { 12 | public String name; 13 | public int father; 14 | 15 | public Vertex(String name) 16 | { 17 | this.name = name; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /graph/PathMatrixProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace PathMatrixProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | DirectedGraph g1 = new DirectedGraph(); 15 | 16 | g1.InsertVertex("Zero"); 17 | g1.InsertVertex("One"); 18 | g1.InsertVertex("Two"); 19 | g1.InsertVertex("Three"); 20 | 21 | g1.InsertEdge("Zero","One"); 22 | g1.InsertEdge("Zero","Three"); 23 | g1.InsertEdge("One","Two"); 24 | g1.InsertEdge("One","Three"); 25 | g1.InsertEdge("Three","Two"); 26 | g1.FindPathMatrix(); 27 | 28 | Console.WriteLine(); 29 | 30 | DirectedGraph g2 = new DirectedGraph(); 31 | 32 | g2.InsertVertex("Zero"); 33 | g2.InsertVertex("One"); 34 | g2.InsertVertex("Two"); 35 | g2.InsertVertex("Three"); 36 | 37 | g2.InsertEdge("Zero","One"); 38 | g2.InsertEdge("Zero","Three"); 39 | g2.InsertEdge("One","Two"); 40 | g2.InsertEdge("One","Three"); 41 | g2.InsertEdge("Two","Zero"); 42 | g2.InsertEdge("Three","Two"); 43 | g2.FindPathMatrix(); 44 | } 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /graph/PathMatrixProject/DirectedGraph.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace PathMatrixProject 9 | { 10 | class DirectedGraph 11 | { 12 | public readonly int MAX_VERTICES = 30; 13 | 14 | int n; 15 | int e; 16 | bool [,] adj; 17 | Vertex [] vertexList; 18 | 19 | public DirectedGraph() 20 | { 21 | adj = new bool[MAX_VERTICES,MAX_VERTICES]; 22 | vertexList = new Vertex[MAX_VERTICES]; 23 | } 24 | 25 | public void FindPathMatrix() 26 | { 27 | int [,] x = new int[n,n]; 28 | int[,] adjp = new int[n, n]; 29 | int[,] temp = new int[n, n]; 30 | 31 | for (int i = 0; i qu = new Queue(); 63 | 64 | qu.Enqueue(v); 65 | vertexList[v].state = WAITING; 66 | 67 | while (qu.Count != 0) 68 | { 69 | v = qu.Dequeue(); 70 | vertexList[v].state = VISITED; 71 | vertexList[v].componentNumber = cN; 72 | 73 | for(int i = 0; i < n; i++) 74 | { 75 | if(IsAdjacent(v,i) && vertexList[i].state == INITIAL) 76 | { 77 | qu.Enqueue(i); 78 | vertexList[i].state = WAITING; 79 | } 80 | } 81 | } 82 | } 83 | 84 | 85 | private int GetIndex(String s) 86 | { 87 | for (int i = 0; i < n; i++) 88 | if ( s.Equals(vertexList[i].name) ) 89 | return i; 90 | throw new InvalidOperationException("Invalid Vertex"); 91 | } 92 | 93 | public void InsertVertex(String name) 94 | { 95 | vertexList[n++] = new Vertex(name); 96 | } 97 | 98 | /* Returns true if edge (s1,s2) exists */ 99 | public bool EdgeExists(String s1, String s2) 100 | { 101 | return IsAdjacent(GetIndex(s1), GetIndex(s2)); 102 | } 103 | 104 | private bool IsAdjacent(int u, int v) 105 | { 106 | return adj[u,v]; 107 | } 108 | 109 | /*Insert an edge (s1,s2) */ 110 | public void InsertEdge(String s1, String s2) 111 | { 112 | int u = GetIndex(s1); 113 | int v = GetIndex(s2); 114 | if(adj[u,v] == true) 115 | Console.Write("Edge already present"); 116 | else 117 | { 118 | adj[u,v]=true; 119 | adj[v,u]=true; 120 | e++; 121 | } 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /graph/breadth-first-search/BFSConnectedProject/Vertex.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BFSConnectedProject 9 | { 10 | class Vertex 11 | { 12 | public String name; 13 | public int state; 14 | public int componentNumber; 15 | 16 | public Vertex(String name) 17 | { 18 | this.name = name; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /graph/breadth-first-search/BFSProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BFSProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | DirectedGraph g = new DirectedGraph(); 15 | 16 | g.InsertVertex("Zero"); 17 | g.InsertVertex("One"); 18 | g.InsertVertex("Two"); 19 | g.InsertVertex("Three"); 20 | g.InsertVertex("Four"); 21 | g.InsertVertex("Five"); 22 | g.InsertVertex("Six"); 23 | g.InsertVertex("Seven"); 24 | g.InsertVertex("Eight"); 25 | g.InsertVertex("Nine"); 26 | 27 | g.InsertEdge("Zero", "One"); 28 | g.InsertEdge("Zero", "Three"); 29 | g.InsertEdge("One", "Two"); 30 | g.InsertEdge("One", "Four"); 31 | g.InsertEdge("One", "Five"); 32 | g.InsertEdge("Two", "Three"); 33 | g.InsertEdge("Two", "Five"); 34 | g.InsertEdge("Three", "Six"); 35 | g.InsertEdge("Four", "Five"); 36 | g.InsertEdge("Four", "Seven"); 37 | g.InsertEdge("Five", "Six"); 38 | g.InsertEdge("Five", "Eight"); 39 | g.InsertEdge("Six", "Eight"); 40 | g.InsertEdge("Six", "Nine"); 41 | g.InsertEdge("Seven", "Eight"); 42 | g.InsertEdge("Eight", "Nine"); 43 | 44 | g.BfsTraversal(); 45 | g.BfsTraversal_All(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /graph/breadth-first-search/BFSProject/DirectedGraph.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | using System.Collections.Generic; 8 | 9 | namespace BFSProject 10 | { 11 | class DirectedGraph 12 | { 13 | public readonly int MAX_VERTICES = 30; 14 | 15 | int n; 16 | int e; 17 | bool [,] adj; 18 | Vertex [] vertexList; 19 | 20 | /* constants for different states of a vertex */ 21 | private readonly int INITIAL = 0; 22 | private readonly int WAITING = 1; 23 | private readonly int VISITED = 2; 24 | 25 | public DirectedGraph() 26 | { 27 | adj = new bool[MAX_VERTICES,MAX_VERTICES]; 28 | vertexList = new Vertex[MAX_VERTICES]; 29 | } 30 | 31 | 32 | public void BfsTraversal() 33 | { 34 | for (int v = 0; v < n; v++) 35 | vertexList[v].state = INITIAL; 36 | 37 | Console.Write("Enter starting vertex for Breadth First Search : "); 38 | String s = Console.ReadLine(); 39 | Bfs( GetIndex(s) ); 40 | } 41 | 42 | private void Bfs(int v) 43 | { 44 | Queue qu = new Queue(); 45 | qu.Enqueue(v); 46 | vertexList[v].state = WAITING; 47 | 48 | while (qu.Count != 0) 49 | { 50 | v = qu.Dequeue(); 51 | Console.Write(vertexList[v].name + " "); 52 | vertexList[v].state = VISITED; 53 | 54 | for (int i = 0; i 1; i-- ) 68 | Console.Write ( vertexList[path[i]].name +"->" ); 69 | Console.WriteLine ( vertexList[path[i]].name ); 70 | } 71 | } 72 | } 73 | 74 | private void Bfs(int v) 75 | { 76 | Queue qu = new Queue(); 77 | qu.Enqueue(v); 78 | vertexList[v].state = WAITING; 79 | vertexList[v].distance = 0; 80 | vertexList[v].predecessor = NIL; 81 | 82 | while ( qu.Count != 0 ) 83 | { 84 | v = qu.Dequeue(); 85 | vertexList[v].state = VISITED; 86 | 87 | for ( int i = 0; i < n; i++) 88 | { 89 | if ( IsAdjacent(v,i) && vertexList[i].state == INITIAL ) 90 | { 91 | qu.Enqueue(i); 92 | vertexList[i].state = WAITING; 93 | vertexList[i].predecessor = v; 94 | vertexList[i].distance = vertexList[v].distance + 1; 95 | } 96 | } 97 | } 98 | Console.WriteLine(); 99 | } 100 | 101 | public void InsertVertex(String name) 102 | { 103 | vertexList[n++] = new Vertex(name); 104 | } 105 | 106 | private int GetIndex(String s) 107 | { 108 | for (int i = 0; i < n; i++) 109 | if (s.Equals(vertexList[i].name)) 110 | return i; 111 | throw new System.InvalidOperationException("Invalid Vertex"); 112 | } 113 | 114 | private bool IsAdjacent(int u, int v) 115 | { 116 | return adj[u, v]; 117 | } 118 | 119 | /*Insert an edge (s1,s2) */ 120 | public void InsertEdge(String s1, String s2) 121 | { 122 | int u = GetIndex(s1); 123 | int v = GetIndex(s2); 124 | 125 | if (u == v) 126 | throw new System.InvalidOperationException("Not a valid edge"); 127 | if (adj[u, v] == true) 128 | Console.Write("Edge already present"); 129 | else 130 | { 131 | adj[u, v] = true; 132 | e++; 133 | } 134 | } 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /graph/breadth-first-search/BFSShortestPathsProject/Vertex.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BFSShortestPathsProject 9 | { 10 | class Vertex 11 | { 12 | public String name; 13 | public int state; 14 | public int predecessor; 15 | public int distance; 16 | 17 | public Vertex(String name) 18 | { 19 | this.name = name; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /graph/breadth-first-search/BFSTreeEdgesProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BFSTreeEdgesProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | DirectedGraph g = new DirectedGraph(); 15 | 16 | g.InsertVertex("Zero"); 17 | g.InsertVertex("One"); 18 | g.InsertVertex("Two"); 19 | g.InsertVertex("Three"); 20 | g.InsertVertex("Four"); 21 | g.InsertVertex("Five"); 22 | g.InsertVertex("Six"); 23 | g.InsertVertex("Seven"); 24 | g.InsertVertex("Eight"); 25 | g.InsertVertex("Nine"); 26 | 27 | g.InsertEdge("Zero", "One"); 28 | g.InsertEdge("Zero", "Three"); 29 | g.InsertEdge("One", "Two"); 30 | g.InsertEdge("One", "Four"); 31 | g.InsertEdge("One", "Five"); 32 | g.InsertEdge("Two", "Three"); 33 | g.InsertEdge("Two", "Five"); 34 | g.InsertEdge("Three", "Six"); 35 | g.InsertEdge("Four", "Five"); 36 | g.InsertEdge("Four", "Seven"); 37 | g.InsertEdge("Five", "Six"); 38 | g.InsertEdge("Five", "Eight"); 39 | g.InsertEdge("Six", "Eight"); 40 | g.InsertEdge("Six", "Nine"); 41 | g.InsertEdge("Seven", "Eight"); 42 | g.InsertEdge("Eight", "Nine"); 43 | 44 | g.BfsTreeEdges(); 45 | 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /graph/breadth-first-search/BFSTreeEdgesProject/DirectedGraph.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | using System.Collections.Generic; 8 | 9 | namespace BFSTreeEdgesProject 10 | { 11 | class DirectedGraph 12 | { 13 | public readonly int MAX_VERTICES = 30; 14 | 15 | int n; 16 | int e; 17 | bool[,] adj; 18 | Vertex[] vertexList; 19 | 20 | /* constants for different states of a vertex */ 21 | private readonly int INITIAL = 0; 22 | private readonly int WAITING = 1; 23 | private readonly int VISITED = 2; 24 | 25 | public DirectedGraph() 26 | { 27 | adj = new bool[MAX_VERTICES, MAX_VERTICES]; 28 | vertexList = new Vertex[MAX_VERTICES]; 29 | } 30 | 31 | public void BfsTreeEdges() 32 | { 33 | int v; 34 | for (v = 0; v < n; v++) 35 | vertexList[v].state = INITIAL; 36 | 37 | Console.Write("Enter starting vertex for Breadth First Search : "); 38 | String s = Console.ReadLine(); 39 | BfsTree( GetIndex(s) ); 40 | 41 | for (v = 0; v < n; v++) 42 | if(vertexList[v].state == INITIAL) 43 | BfsTree(v); 44 | } 45 | 46 | private void BfsTree(int v) 47 | { 48 | Queue qu = new Queue(); 49 | qu.Enqueue(v); 50 | vertexList[v].state = WAITING; 51 | while ( qu.Count!=0 ) 52 | { 53 | v = qu.Dequeue(); 54 | vertexList[v].state = VISITED; 55 | 56 | for(int i = 0; i < n; i++) 57 | { 58 | if(IsAdjacent(v,i) && vertexList[i].state == INITIAL) 59 | { 60 | qu.Enqueue(i); 61 | vertexList[i].state=WAITING; 62 | Console.WriteLine("Tree Edge : (" + 63 | vertexList[v].name + "," + vertexList[i].name + ")" ); 64 | } 65 | } 66 | } 67 | Console.WriteLine(); 68 | } 69 | 70 | 71 | public void BfsTraversal() 72 | { 73 | for (int v = 0; v < n; v++) 74 | vertexList[v].state = INITIAL; 75 | 76 | Console.Write("Enter starting vertex for Breadth First Search : "); 77 | String s = Console.ReadLine(); 78 | Bfs ( GetIndex(s) ); 79 | } 80 | 81 | private void Bfs(int v) 82 | { 83 | Queue qu = new Queue(); 84 | qu.Enqueue(v); 85 | vertexList[v].state = WAITING; 86 | 87 | while (qu.Count != 0) 88 | { 89 | v = qu.Dequeue(); 90 | Console.Write( vertexList[v].name + " "); 91 | vertexList[v].state = VISITED; 92 | 93 | for (int i = 0; i < n; i++) 94 | { 95 | if (IsAdjacent(v, i) && vertexList[i].state == INITIAL) 96 | { 97 | qu.Enqueue(i); 98 | vertexList[i].state = WAITING; 99 | } 100 | } 101 | } 102 | Console.WriteLine(); 103 | } 104 | 105 | public void BfsTraversal_All() 106 | { 107 | int v; 108 | for (v = 0; v < n; v++) 109 | vertexList[v].state = INITIAL; 110 | 111 | Console.Write("Enter starting vertex for Breadth First Search : "); 112 | String s = Console.ReadLine(); 113 | Bfs( GetIndex(s) ); 114 | 115 | for (v = 0; v < n; v++) 116 | if (vertexList[v].state == INITIAL) 117 | Bfs(v); 118 | } 119 | 120 | 121 | public void InsertVertex(String name) 122 | { 123 | vertexList[n++] = new Vertex(name); 124 | } 125 | 126 | private int GetIndex(String s) 127 | { 128 | for (int i = 0; i < n; i++) 129 | if (s.Equals(vertexList[i].name)) 130 | return i; 131 | throw new System.InvalidOperationException("Invalid Vertex"); 132 | } 133 | 134 | private bool IsAdjacent(int u, int v) 135 | { 136 | return adj[u, v]; 137 | } 138 | 139 | /*Insert an edge (s1,s2) */ 140 | public void InsertEdge(String s1, String s2) 141 | { 142 | int u = GetIndex(s1); 143 | int v = GetIndex(s2); 144 | 145 | if (u == v) 146 | throw new System.InvalidOperationException("Not a valid edge"); 147 | if (adj[u, v] == true) 148 | Console.Write("Edge already present"); 149 | else 150 | { 151 | adj[u, v] = true; 152 | e++; 153 | } 154 | } 155 | 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /graph/breadth-first-search/BFSTreeEdgesProject/Vertex.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BFSTreeEdgesProject 9 | { 10 | class Vertex 11 | { 12 | public String name; 13 | public int state; 14 | 15 | public Vertex(String name) 16 | { 17 | this.name = name; 18 | } 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /graph/breadth-first-search/BFSUndirectedProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BFSUndirectedProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | UndirectedGraph g = new UndirectedGraph(); 15 | 16 | g.InsertVertex("Zero"); 17 | g.InsertVertex("One"); 18 | g.InsertVertex("Two"); 19 | g.InsertVertex("Three"); 20 | g.InsertVertex("Four"); 21 | g.InsertVertex("Five"); 22 | g.InsertVertex("Six"); 23 | g.InsertVertex("Seven"); 24 | g.InsertVertex("Eight"); 25 | g.InsertVertex("Nine"); 26 | 27 | g.InsertEdge("Zero", "One"); 28 | g.InsertEdge("Zero", "Seven"); 29 | g.InsertEdge("One", "Two"); 30 | g.InsertEdge("One", "Four"); 31 | g.InsertEdge("One", "Five"); 32 | g.InsertEdge("Two", "Three"); 33 | g.InsertEdge("Two", "Five"); 34 | g.InsertEdge("Three", "Six"); 35 | g.InsertEdge("Four", "Seven"); 36 | g.InsertEdge("Five", "Six"); 37 | g.InsertEdge("Five", "Seven"); 38 | g.InsertEdge("Five", "Eight"); 39 | g.InsertEdge("Six", "Nine"); 40 | g.InsertEdge("Seven", "Eight"); 41 | g.InsertEdge("Eight", "Nine"); 42 | 43 | g.BfsTraversal(); 44 | g.BfsTraversal_All(); 45 | 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /graph/breadth-first-search/BFSUndirectedProject/UndirectedGraph.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | using System.Collections.Generic; 8 | 9 | namespace BFSUndirectedProject 10 | { 11 | class UndirectedGraph 12 | { 13 | public readonly int MAX_VERTICES = 30; 14 | 15 | int n; 16 | int e; 17 | bool [,] adj; 18 | Vertex [] vertexList; 19 | 20 | /*constants for different states of a vertex*/ 21 | private readonly int INITIAL = 0; 22 | private readonly int WAITING = 1; 23 | private readonly int VISITED = 2; 24 | 25 | public UndirectedGraph() 26 | { 27 | adj = new bool[MAX_VERTICES,MAX_VERTICES]; 28 | vertexList = new Vertex[MAX_VERTICES]; 29 | } 30 | 31 | public void BfsTraversal() 32 | { 33 | int v; 34 | for ( v = 0; v < n; v++) 35 | vertexList[v].state = INITIAL; 36 | 37 | Console.Write("Enter starting vertex for Breadth First Search : "); 38 | String s = Console.ReadLine(); 39 | Bfs( GetIndex(s) ); 40 | } 41 | 42 | private void Bfs(int v) 43 | { 44 | Queue qu = new Queue(); 45 | qu.Enqueue(v); 46 | vertexList[v].state = WAITING; 47 | while ( qu.Count!=0 ) 48 | { 49 | v = qu.Dequeue(); 50 | Console.Write(vertexList[v].name + " "); 51 | vertexList[v].state = VISITED; 52 | 53 | for (int i = 0; i < n; i++) 54 | { 55 | if(IsAdjacent(v,i) && vertexList[i].state == INITIAL) 56 | { 57 | qu.Enqueue(i); 58 | vertexList[i].state=WAITING; 59 | } 60 | } 61 | } 62 | Console.WriteLine(); 63 | } 64 | 65 | 66 | public void BfsTraversal_All() 67 | { 68 | int v; 69 | for ( v = 0; v < n; v++) 70 | vertexList[v].state = INITIAL; 71 | 72 | Console.Write("Enter starting vertex for Breadth First Search : "); 73 | String s = Console.ReadLine(); 74 | Bfs(GetIndex(s)); 75 | 76 | for ( v = 0; v < n; v++ ) 77 | if ( vertexList[v].state == INITIAL ) 78 | Bfs(v); 79 | } 80 | 81 | 82 | private int GetIndex(String s) 83 | { 84 | for(int i=0; i st = new Stack(); 43 | st.Push(v); 44 | 45 | while ( st.Count!=0 ) 46 | { 47 | v = st.Pop(); 48 | 49 | if ( vertexList[v].state == INITIAL ) 50 | { 51 | Console.Write(vertexList[v].name + " "); 52 | vertexList[v].state=VISITED; 53 | } 54 | 55 | for ( int i = n-1; i >= 0; i--) 56 | { 57 | if ( IsAdjacent(v,i) && vertexList[i].state == INITIAL ) 58 | st.Push(i); 59 | } 60 | } 61 | Console.WriteLine(); 62 | } 63 | 64 | public void DfsTraversal_All() 65 | { 66 | int v; 67 | for ( v = 0; v < n; v++) 68 | vertexList[v].state = INITIAL; 69 | 70 | Console.Write("Enter starting vertex for Depth First Search : "); 71 | String s = Console.ReadLine(); 72 | Dfs ( GetIndex(s) ); 73 | 74 | for ( v = 0; v < n; v++) 75 | if(vertexList[v].state == INITIAL) 76 | Dfs(v); 77 | } 78 | 79 | public void InsertVertex(String name) 80 | { 81 | vertexList[n++] = new Vertex(name); 82 | } 83 | 84 | private int GetIndex(String s) 85 | { 86 | for (int i = 0; i < n; i++) 87 | if (s.Equals(vertexList[i].name)) 88 | return i; 89 | throw new System.InvalidOperationException("Invalid Vertex"); 90 | } 91 | 92 | private bool IsAdjacent(int u, int v) 93 | { 94 | return adj[u, v]; 95 | } 96 | 97 | /*Insert an edge (s1,s2) */ 98 | public void InsertEdge(String s1, String s2) 99 | { 100 | int u = GetIndex(s1); 101 | int v = GetIndex(s2); 102 | 103 | if (u == v) 104 | throw new System.InvalidOperationException("Not a valid edge"); 105 | if (adj[u, v] == true) 106 | Console.Write("Edge already present"); 107 | else 108 | { 109 | adj[u, v] = true; 110 | e++; 111 | } 112 | } 113 | 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /graph/depth-first-search/DFSProject/Vertex.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace DFSProject 9 | { 10 | class Vertex 11 | { 12 | public String name; 13 | public int state; 14 | 15 | public Vertex(String name) 16 | { 17 | this.name = name; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /graph/depth-first-search/DFSRecursiveProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace DFSRecursiveProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | DirectedGraph g = new DirectedGraph(); 15 | 16 | g.InsertVertex("Zero"); 17 | g.InsertVertex("One"); 18 | g.InsertVertex("Two"); 19 | g.InsertVertex("Three"); 20 | g.InsertVertex("Four"); 21 | g.InsertVertex("Five"); 22 | g.InsertVertex("Six"); 23 | g.InsertVertex("Seven"); 24 | g.InsertVertex("Eight"); 25 | g.InsertVertex("Nine"); 26 | g.InsertVertex("Ten"); 27 | g.InsertVertex("Eleven"); 28 | 29 | g.InsertEdge("Zero", "One"); 30 | g.InsertEdge("Zero", "Three"); 31 | g.InsertEdge("One", "Two"); 32 | g.InsertEdge("One", "Four"); 33 | g.InsertEdge("One", "Five"); 34 | g.InsertEdge("Two", "Five"); 35 | g.InsertEdge("Two", "Seven"); 36 | g.InsertEdge("Three", "Six"); 37 | g.InsertEdge("Four", "Three"); 38 | g.InsertEdge("Five", "Three"); 39 | g.InsertEdge("Five", "Six"); 40 | g.InsertEdge("Five", "Eight"); 41 | g.InsertEdge("Seven", "Eight"); 42 | g.InsertEdge("Seven", "Ten"); 43 | g.InsertEdge("Eight", "Eleven"); 44 | g.InsertEdge("Nine", "Six"); 45 | g.InsertEdge("Eleven", "Nine"); 46 | 47 | g.DfsTraversal(); 48 | g.DfsTraversal_All(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /graph/depth-first-search/DFSRecursiveProject/DirectedGraph.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | using System.Collections.Generic; 8 | 9 | namespace DFSRecursiveProject 10 | { 11 | class DirectedGraph 12 | { 13 | public readonly int MAX_VERTICES = 30; 14 | 15 | int n; 16 | int e; 17 | bool[,] adj; 18 | Vertex[] vertexList; 19 | 20 | /* constants for different states of a vertex */ 21 | private readonly int INITIAL = 0; 22 | private readonly int VISITED = 1; 23 | private readonly int FINISHED = 2; 24 | 25 | public DirectedGraph() 26 | { 27 | adj = new bool[MAX_VERTICES, MAX_VERTICES]; 28 | vertexList = new Vertex[MAX_VERTICES]; 29 | } 30 | 31 | public void DfsTraversal() 32 | { 33 | for (int v = 0; v < n; v++) 34 | vertexList[v].state = INITIAL; 35 | 36 | Console.Write("Enter starting vertex for Depth First Search : "); 37 | String s = Console.ReadLine(); 38 | Dfs( GetIndex(s) ); 39 | Console.WriteLine(); 40 | } 41 | 42 | private void Dfs(int v) 43 | { 44 | Console.Write(vertexList[v].name + " "); 45 | vertexList[v].state = VISITED; 46 | 47 | for(int i=0; i st = new Stack(); 64 | st.Push(v); 65 | while ( st.Count!=0 ) 66 | { 67 | v = st.Pop(); 68 | if ( vertexList[v].state == INITIAL ) 69 | { 70 | Console.Write(vertexList[v].name + " "); 71 | vertexList[v].state=VISITED; 72 | } 73 | for ( int i = n-1; i>=0; i-- ) 74 | { 75 | if ( IsAdjacent(v,i) && vertexList[i].state == INITIAL ) 76 | { 77 | st.Push(i); 78 | vertexList[i].predecessor = v; 79 | } 80 | } 81 | } 82 | Console.WriteLine(); 83 | } 84 | 85 | public void DfsTraversal() 86 | { 87 | for ( int v = 0; v < n; v++ ) 88 | vertexList[v].state = INITIAL; 89 | 90 | Console.Write("Enter starting vertex for Depth First Search : "); 91 | String s = Console.ReadLine(); 92 | Dfs ( GetIndex(s) ); 93 | } 94 | 95 | private void Dfs(int v) 96 | { 97 | Stack st = new Stack(); 98 | st.Push(v); 99 | while ( st.Count != 0 ) 100 | { 101 | v = st.Pop(); 102 | if (vertexList[v].state == INITIAL) 103 | { 104 | Console.Write(vertexList[v].name + " "); 105 | vertexList[v].state = VISITED; 106 | } 107 | for (int i = n-1; i >= 0; i--) 108 | { 109 | if (IsAdjacent(v,i) && vertexList[i].state == INITIAL) 110 | st.Push(i); 111 | } 112 | } 113 | Console.WriteLine(); 114 | } 115 | 116 | public void DfsTraversal_All() 117 | { 118 | int v; 119 | for (v = 0; v < n; v++) 120 | vertexList[v].state = INITIAL; 121 | 122 | Console.Write("Enter starting vertex for Depth First Search : "); 123 | String s = Console.ReadLine(); 124 | Dfs(GetIndex(s)); 125 | 126 | for (v = 0; v < n; v++) 127 | if (vertexList[v].state == INITIAL) 128 | Dfs(v); 129 | } 130 | 131 | public void InsertVertex(String name) 132 | { 133 | vertexList[n++] = new Vertex(name); 134 | } 135 | 136 | private int GetIndex(String s) 137 | { 138 | for (int i = 0; i < n; i++) 139 | if (s.Equals(vertexList[i].name)) 140 | return i; 141 | throw new System.InvalidOperationException("Invalid Vertex"); 142 | } 143 | 144 | private bool IsAdjacent(int u, int v) 145 | { 146 | return adj[u, v]; 147 | } 148 | 149 | /*Insert an edge (s1,s2) */ 150 | public void InsertEdge(String s1, String s2) 151 | { 152 | int u = GetIndex(s1); 153 | int v = GetIndex(s2); 154 | 155 | if (u == v) 156 | throw new System.InvalidOperationException("Not a valid edge"); 157 | if (adj[u, v] == true) 158 | Console.Write("Edge already present"); 159 | else 160 | { 161 | adj[u, v] = true; 162 | e++; 163 | } 164 | } 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /graph/depth-first-search/DFSTreeEdgesProject/Vertex.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Text; 10 | 11 | namespace DFSTreeEdgesProject 12 | { 13 | class Vertex 14 | { 15 | public String name; 16 | public int state; 17 | public int predecessor; 18 | 19 | public Vertex(String name) 20 | { 21 | this.name = name; 22 | } 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /graph/depth-first-search/DFSUndirectedProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace DFSUndirectedProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | UndirectedGraph g = new UndirectedGraph(); 15 | 16 | g.InsertVertex("Zero"); 17 | g.InsertVertex("One"); 18 | g.InsertVertex("Two"); 19 | g.InsertVertex("Three"); 20 | g.InsertVertex("Four"); 21 | g.InsertVertex("Five"); 22 | g.InsertVertex("Six"); 23 | g.InsertVertex("Seven"); 24 | g.InsertVertex("Eight"); 25 | g.InsertVertex("Nine"); 26 | g.InsertVertex("Ten"); 27 | g.InsertVertex("Eleven"); 28 | g.InsertVertex("Twelve"); 29 | g.InsertVertex("Thirteen"); 30 | g.InsertVertex("Fourteen"); 31 | 32 | g.InsertEdge("Zero","One"); 33 | g.InsertEdge("Zero","Three"); 34 | g.InsertEdge("One","Two"); 35 | g.InsertEdge("One","Three"); 36 | g.InsertEdge("One","Four"); 37 | g.InsertEdge("Three","Four"); 38 | g.InsertEdge("Five","Six"); 39 | g.InsertEdge("Five","Seven"); 40 | g.InsertEdge("Five","Eight"); 41 | g.InsertEdge("Seven","Eight"); 42 | g.InsertEdge("Nine","Ten"); 43 | g.InsertEdge("Nine","Eleven"); 44 | g.InsertEdge("Nine","Twelve"); 45 | g.InsertEdge("Nine","Thirteen"); 46 | g.InsertEdge("Ten","Twelve"); 47 | g.InsertEdge("Eleven","Thirteen"); 48 | g.InsertEdge("Eleven","Fourteen"); 49 | 50 | g.DfsTraversal(); 51 | 52 | 53 | g.DfsTraversal_All(); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /graph/depth-first-search/DFSUndirectedProject/UndirectedGraph.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace DFSUndirectedProject 9 | { 10 | class UndirectedGraph 11 | { 12 | public readonly int MAX_VERTICES = 30; 13 | 14 | int n; 15 | int e; 16 | bool [,] adj; 17 | Vertex [] vertexList; 18 | 19 | private readonly int INITIAL = 0; 20 | private readonly int VISITED = 1; 21 | private readonly int FINISHED = 2; 22 | 23 | public UndirectedGraph() 24 | { 25 | adj = new bool[MAX_VERTICES,MAX_VERTICES]; 26 | vertexList = new Vertex[MAX_VERTICES]; 27 | } 28 | 29 | public void DfsTraversal() 30 | { 31 | int v; 32 | for ( v = 0; v < n; v++ ) 33 | vertexList[v].state = INITIAL; 34 | 35 | Console.Write("Enter starting vertex for Depth First Search : "); 36 | String s = Console.ReadLine(); 37 | Dfs( GetIndex(s) ); 38 | 39 | Console.WriteLine(); 40 | } 41 | 42 | private void Dfs(int v) 43 | { 44 | Console.Write(vertexList[v].name + " "); 45 | vertexList[v].state = VISITED; 46 | 47 | for(int i=0; i= 0; i--) 86 | Console.WriteLine(stackArray[i] + " "); 87 | Console.WriteLine(); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /trees/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C# 2 | 3 | This [“Advanced Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes AVL tree, BTree, Graph Algorithms 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 20 | [![data-structures- and-algorithms-in-python-2](https://user-images.githubusercontent.com/98641125/153196106-0eb1a386-c36b-4f14-8675-9d865438f882.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-python-2/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /trees/ThreadedTreeProject/Demo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ThreadedTreeProject 4 | { 5 | class Demo 6 | { 7 | static void Main(string[] args) 8 | { 9 | ThreadedBinaryTree tree = new ThreadedBinaryTree(); 10 | int choice,x; 11 | 12 | while(true) 13 | { 14 | Console.WriteLine("1.Insert a new node"); 15 | Console.WriteLine("2.Delete a node"); 16 | Console.WriteLine("3.Inorder Traversal"); 17 | Console.WriteLine("4.Exit"); 18 | 19 | Console.Write("Enter your choice : "); 20 | choice = Convert.ToInt32(Console.ReadLine()); 21 | 22 | if(choice == 4) 23 | break; 24 | 25 | switch( choice ) 26 | { 27 | case 1: 28 | Console.Write("Enter the key to be inserted : "); 29 | x = Convert.ToInt32(Console.ReadLine()); 30 | tree.Insert(x); 31 | break; 32 | case 2: 33 | Console.Write("Enter the key to be deleted : "); 34 | x = Convert.ToInt32(Console.ReadLine()); 35 | tree.Delete(x); 36 | break; 37 | case 3: 38 | tree.Inorder(); 39 | break; 40 | } 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /trees/ThreadedTreeProject/Node.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ThreadedTreeProject 4 | { 5 | class Node 6 | { 7 | public Node left; 8 | public bool leftThread; 9 | public int info; 10 | public bool rightThread; 11 | public Node right; 12 | 13 | public Node(int i) 14 | { 15 | info = i; 16 | left = null; 17 | right = null; 18 | leftThread = true; 19 | rightThread = true; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /trees/ThreadedTreeProject/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C# 2 | 3 | This [“Advanced Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes AVL tree, BTree, Graph Algorithms 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 20 | [![data-structures- and-algorithms-in-python-2](https://user-images.githubusercontent.com/98641125/153196106-0eb1a386-c36b-4f14-8675-9d865438f882.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-python-2/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /trees/ThreadedTreeProject/ThreadedBinaryTree.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ThreadedTreeProject 4 | { 5 | class ThreadedBinaryTree 6 | { 7 | private Node root; 8 | 9 | public ThreadedBinaryTree() 10 | { 11 | root = null; 12 | } 13 | 14 | public bool IsEmpty() 15 | { 16 | return (root == null); 17 | } 18 | 19 | private Node InorderPredecessor(Node p) 20 | { 21 | if(p.leftThread == true) 22 | return p.left; 23 | else 24 | { 25 | p = p.left; 26 | while(p.rightThread == false) 27 | p = p.right; 28 | return p; 29 | } 30 | } 31 | 32 | private Node InorderSuccessor(Node p) 33 | { 34 | if(p.rightThread == true) 35 | return p.right; 36 | else 37 | { 38 | p = p.right; 39 | while(p.leftThread == false) 40 | p = p.left; 41 | return p; 42 | } 43 | } 44 | 45 | public void Inorder() 46 | { 47 | if(root == null ) 48 | { 49 | Console.Write("Tree is empty"); 50 | return; 51 | } 52 | 53 | /*Find the leftmost node of the tree*/ 54 | Node p = root; 55 | while( p.leftThread == false ) 56 | p = p.left; 57 | 58 | while( p!=null ) 59 | { 60 | Console.Write(p.info + " "); 61 | if (p.rightThread == true) 62 | p = p.right; 63 | else 64 | { 65 | p = p.right; 66 | while (p.leftThread == false) 67 | p = p.left; 68 | } 69 | } 70 | Console.WriteLine(); 71 | } 72 | 73 | public void Insert(int x) 74 | { 75 | Node p = root; 76 | Node par = null; 77 | 78 | while (p!=null) 79 | { 80 | par = p; 81 | if (x < p.info) 82 | { 83 | if (p.leftThread == false) 84 | p = p.left; 85 | else 86 | break; 87 | } 88 | else if(x > p.info) 89 | { 90 | if (p.rightThread == false) 91 | p = p.right; 92 | else 93 | break; 94 | } 95 | else 96 | { 97 | Console.WriteLine(x + " already present in the tree"); 98 | return; 99 | } 100 | } 101 | 102 | Node temp = new Node(x); 103 | if (par == null) 104 | { 105 | root = temp; 106 | } 107 | else if (x < par.info) /*inserted as left child*/ 108 | { 109 | temp.left = par.left; 110 | temp.right = par; 111 | par.leftThread = false; 112 | par.left = temp; 113 | } 114 | else /*inserted as right child*/ 115 | { 116 | temp.left = par; 117 | temp.right = par.right; 118 | par.rightThread = false; 119 | par.right = temp; 120 | } 121 | } 122 | 123 | public void Delete(int x) 124 | { 125 | Node p = root; 126 | Node par = null; 127 | 128 | while (p != null) 129 | { 130 | if (x == p.info) 131 | break; 132 | par = p; 133 | if (x < p.info) 134 | { 135 | if (p.leftThread == false) 136 | p = p.left; 137 | else 138 | break; 139 | } 140 | else 141 | { 142 | if (p.rightThread == false) 143 | p = p.right; 144 | else 145 | break; 146 | } 147 | } 148 | 149 | if (p == null || p.info != x) 150 | { 151 | Console.WriteLine(x + " not found"); 152 | return; 153 | } 154 | 155 | if (p.leftThread == false && p.rightThread == false)/*Case C: 2 children*/ 156 | { 157 | /*Find inorder successor and its parent*/ 158 | Node ps = p; 159 | Node s = p.right; 160 | 161 | while (s.leftThread == false) 162 | { 163 | ps = s; 164 | s = s.left; 165 | } 166 | p.info = s.info; 167 | p = s; 168 | par = ps; 169 | } 170 | 171 | /*Case A : No child*/ 172 | if (p.leftThread == true && p.rightThread == true) 173 | { 174 | if (par == null) 175 | root = null; 176 | else if (p == par.left) 177 | { 178 | par.leftThread = true; 179 | par.left = p.left; 180 | } 181 | else 182 | { 183 | par.rightThread = true; 184 | par.right = p.right; 185 | } 186 | return; 187 | } 188 | 189 | /*Case B : 1 child*/ 190 | Node ch; 191 | if (p.leftThread == false) /*node to be deleted has left child */ 192 | ch = p.left; 193 | else /*node to be deleted has right child */ 194 | ch = p.right; 195 | 196 | if (par == null) /*node to be deleted is root node*/ 197 | root = ch; 198 | else if (p == par.left)/*node is left child of its parent*/ 199 | par.left = ch; 200 | else /*node is right child of its parent*/ 201 | par.right = ch; 202 | 203 | Node pred = InorderPredecessor(p); 204 | Node succ = InorderSuccessor(p); 205 | 206 | if (p.leftThread == false) /*if p has left child, right is a thread */ 207 | pred.right = succ; 208 | else /*p has right child,left is a thread*/ 209 | succ.left = pred; 210 | } 211 | } 212 | } 213 | --------------------------------------------------------------------------------