{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/graphTraversalTechniques","result":{"data":{"blog":{"frontmatter":{"title":"GRAPH TRAVERSAL TECHNIQUES","thumbnail":"blog45","date":"January 27, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/1bmZ6E7JfB_iNv6d243coP0kEkmcQEksu/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <p>\n                To solve problems on graphs, we need a mechanism for traversing\n                the graphs. Graph traversal algorithms are also called graph\n                search algorithms.\n              </p>\n              <p>\n                Like tree traversal algorithms (Inorder, Preorder, Postorder and\n                Level-Order traversals), graph traversal algorithms can be\n                thought of as starting at some source vertex in a graph and\n                \"searching/traversing\" the graph by going through the edges and\n                marking the vertices.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Note:</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  We will be using 2D arrays(adjacency matrix) for implementing\n                  graph.\n                </li>\n                <li>We shall implement an undirected graph.</li>\n                <li>\n                  Make a point that the order of traversing actually can be\n                  anything like ascending or descending or random, but will\n                  stick to traverse each node in ascending order.\n                </li>\n                <li>\n                  The output for DFS & BFS will differ because it depends on\n                  what vertex you start with and in what order you traverse\n                  them.\n                </li>\n              </ul>\n              <p>\n                Now, we will discuss two algorithms for traversing the graphs.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Breadth First Search(BFS)</h4>\n              <div class=\"m-2\">\n                <p>\n                  The BFS algorithm for graphs works similar to BFS algorithm\n                  for trees. Like BFS traversal of trees, internally this\n                  algorithm also uses queue. First create a new array called\n                  isVisited[] which tracks whether a node is visited or not.\n                </p>\n                <p>\n                  Initially we can start from any vertex but for convenience\n                  will start with 0. Print this node, set it as visited and\n                  explore this currently visited node i.e 0. (Exploring means if\n                  there is node adjacent to 0 then add them in the queue in\n                  ascending order).\n                </p>\n                <p>\n                  Now move to the next element in the queue by dequeuing the\n                  front element and explore them as they come in order(queue).\n                  Stop when all the nodes are visited.\n                </p>\n                <p>\n                  <strong> Applications: </strong>\n                  Finding all connected components in a graph, finding all nodes\n                  within one connected component, finding the shortest path\n                  between two nodes, testing a graph for bi-partiteness.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Depth First Search(DFS)</h4>\n              <div class=\"m-2\">\n                <p>\n                  The DFS algorithm for graphs works similar to pre order(type of\n                  DFS) algorithm for trees. Like pre order traversal, internally\n                  this algorithm also uses stack. First create a new array\n                  called isVisited[] which tracks whether a node is visited or\n                  not.\n                </p>\n                <p>\n                  Initially we can start from any vertex but for convenience\n                  will start with 0. Print this node, set it as visited and\n                  visit next adjacent node.\n                </p>\n                <p>\n                  Follow the above procedure until you reach a deadend. Deadend\n                  means, you are just traversing in one way(not exploring),\n                  diving deeper by deeper as you traverse each node and going to\n                  the depth of the graph. So you come across a node whose\n                  adjacent nodes are already visited.\n                </p>\n                <p>\n                  After you reach this deadend, start backtracking. For this\n                  operation will have to use stack but as we are using\n                  recursion, we shall not worry about using stacks.\n                </p>\n                <p>\n                  While backtracking, if you find any node's adjacent node is\n                  unvisited, then traverse to that node and go further deep.\n                </p>\n                <p>\n                  <strong> Applications: </strong>\n                  Topological sorting, finding articulation points (cut\n                  vertices) of the graph, finding strongly connected components,\n                  solving puzzles such as mazes.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Insertion</h4>\n              <ol class=\"pl-4\">\n                <li>\n                  <strong>Adding vertices: </strong>\n                  Adding a vertex will be performed by incrementing the total\n                  no. of vertices(V). The initial value of V will be -1 and when\n                  you add one vertex, this V gets incremented and the\n                  incremented value is treated as the value of new node. <br />\n                  Suppose you add one vertex when V=-1. Now V gets incremented\n                  to 0. So the new vertex is 0.\n                </li>\n                <li>\n                  <strong> Adding edges: </strong>\n                  Take two vertices as two inputs and set the value of this\n                  inputs as 1 in the adjacency matrix.\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/graph\">Graph Data Structure</a> |\n              <a href=\"/blogs/topologicalSort\">Topological Sort</a>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <p>\n                To solve problems on graphs, we need a mechanism for traversing\n                the graphs. Graph traversal algorithms are also called graph\n                search algorithms.\n              </p>\n              <p>\n                Like tree traversal algorithms (Inorder, Preorder, Postorder and\n                Level-Order traversals), graph traversal algorithms can be\n                thought of as starting at some source vertex in a graph and\n                \"searching/traversing\" the graph by going through the edges and\n                marking the vertices.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Note:</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  We will be using 2D arrays(adjacency matrix) for implementing\n                  graph.\n                </li>\n                <li>We shall implement an undirected graph.</li>\n                <li>\n                  Make a point that the order of traversing actually can be\n                  anything like ascending or descending or random, but will\n                  stick to traverse each node in ascending order.\n                </li>\n                <li>\n                  The output for DFS & BFS will differ because it depends on\n                  what vertex you start with and in what order you traverse\n                  them.\n                </li>\n              </ul>\n              <p>\n                Now, we will discuss two algorithms for traversing the graphs.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Breadth First Search(BFS)</h4>\n              <div class=\"m-2\">\n                <p>\n                  The BFS algorithm for graphs works similar to BFS algorithm\n                  for trees. Like BFS traversal of trees, internally this\n                  algorithm also uses queue. First create a new array called\n                  isVisited[] which tracks whether a node is visited or not.\n                </p>\n                <p>\n                  Initially we can start from any vertex but for convenience\n                  will start with 0. Print this node, set it as visited and\n                  explore this currently visited node i.e 0. (Exploring means if\n                  there is node adjacent to 0 then add them in the queue in\n                  ascending order).\n                </p>\n                <p>\n                  Now move to the next element in the queue by dequeuing the\n                  front element and explore them as they come in order(queue).\n                  Stop when all the nodes are visited.\n                </p>\n                <p>\n                  <strong> Applications: </strong>\n                  Finding all connected components in a graph, finding all nodes\n                  within one connected component, finding the shortest path\n                  between two nodes, testing a graph for bi-partiteness.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Depth First Search(DFS)</h4>\n              <div class=\"m-2\">\n                <p>\n                  The DFS algorithm for graphs works similar to pre order(type of\n                  DFS) algorithm for trees. Like pre order traversal, internally\n                  this algorithm also uses stack. First create a new array\n                  called isVisited[] which tracks whether a node is visited or\n                  not.\n                </p>\n                <p>\n                  Initially we can start from any vertex but for convenience\n                  will start with 0. Print this node, set it as visited and\n                  visit next adjacent node.\n                </p>\n                <p>\n                  Follow the above procedure until you reach a deadend. Deadend\n                  means, you are just traversing in one way(not exploring),\n                  diving deeper by deeper as you traverse each node and going to\n                  the depth of the graph. So you come across a node whose\n                  adjacent nodes are already visited.\n                </p>\n                <p>\n                  After you reach this deadend, start backtracking. For this\n                  operation will have to use stack but as we are using\n                  recursion, we shall not worry about using stacks.\n                </p>\n                <p>\n                  While backtracking, if you find any node's adjacent node is\n                  unvisited, then traverse to that node and go further deep.\n                </p>\n                <p>\n                  <strong> Applications: </strong>\n                  Topological sorting, finding articulation points (cut\n                  vertices) of the graph, finding strongly connected components,\n                  solving puzzles such as mazes.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Insertion</h4>\n              <ol class=\"pl-4\">\n                <li>\n                  <strong>Adding vertices: </strong>\n                  Adding a vertex will be performed by incrementing the total\n                  no. of vertices(V). The initial value of V will be -1 and when\n                  you add one vertex, this V gets incremented and the\n                  incremented value is treated as the value of new node. <br />\n                  Suppose you add one vertex when V=-1. Now V gets incremented\n                  to 0. So the new vertex is 0.\n                </li>\n                <li>\n                  <strong> Adding edges: </strong>\n                  Take two vertices as two inputs and set the value of this\n                  inputs as 1 in the adjacency matrix.\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/graph\">Graph Data Structure</a> |\n              <a href=\"/blogs/topologicalSort\">Topological Sort</a>\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M28%2091c-8%203-7%2019%201%2019%205%200%209-10%203-11-2%200-5%204-3%205v1c-1%202-2%200-2-3%200-4%202-7%203-6v1l2%201c3%200%204-2%203-5-1-2-4-3-7-2m13%200c-4%200-4%200-4%203l-1%2010c0%206%200%206%202%206s3-1%203-3c0-4%201-4%203%200%201%203%201%203%204%202%202-1%202-2%200-4v-5c4-6%201-10-7-9m18%200c-2%200-6%209-7%2016-1%203-1%203%201%203l4-2%203-2%201%202c0%202%201%202%206%202s5%200%205-3c0-2%200-3%202-3%203%200%207-4%207-7%200-4-2-6-6-6-6%200-7%201-8%2010v8l-2-7c-2-11-2-12-6-11m24%204l-1%2010c0%205%200%205%202%205%203%200%203-1%203-4%201-5%204-6%204%200%200%203%200%204%202%204s3-1%203-10c0-10%200-10-2-10s-3%201-3%204l-2%204-1-4c0-3-1-4-3-4-1%200-2%201-2%205m27-4c-5%200-5%200-5%203l2%202c2%200%202%201%202%207%200%207%200%207%203%207%202%200%202%200%202-7%200-8%200-8%203-8%202%200%202%200%202-2-1-3-2-3-9-2m15%200c-5%200-5%200-5%2010l-1%209h2c3%200%203%200%203-3l1-3%202%203c1%203%201%203%204%202%202-1%202-2%200-4v-5c5-5%201-10-6-9m17%200l-3%202-5%2016c0%202%205%201%206-1l3-2%201%202c0%202%201%202%203%202h3l-4-17c0-2-2-3-4-2m9%200l1%2010c2%209%202%209%205%209%202%200%203-1%205-9l3-10c-2-2-5%200-6%204l-2%205-1-5c-1-4-4-6-5-4m20%200h-5v4l-1%2010v5h5c6%200%208-1%208-3s-1-2-3-2l-4-1c-1-1%201-2%204-2l2-2c0-2-1-2-3-2-4%200-4-2%201-3%203%200%204-1%204-3l-2-2-6%201m14%200c-5%200-5%201-5%2010%200%208%200%209%202%209s2%200%202-3c0-4%202-4%204%200%201%203%201%203%203%202s2-1%201-4v-5c3-7%200-10-7-9m30%200l-2%202-3%209c-4%209-4%208%200%208l3-1%202-2c2-1%202-1%202%201l4%202c3%200%202%200%200-13-1-5-3-7-6-6m10%200l-1%2010v9h5l7-1h2c8%204%2014-4%207-10l-3-4%202%201c2%203%204%201%204-2%200-2-1-3-4-3-7%200-10%207-4%2011%204%203%204%204%200%204l-4-1-3-1h-4v-7c0-6%200-7-2-7l-2%201m-28%202c-3%203-2%206%201%208%204%204%204%205%200%205-4-1-5%202-1%203%203%201%206%201%208-1%203-3%203-5-1-8l-3-5%201%201c1%202%204%202%205%200%201-5-8-7-10-3m71%209c-3%203-5%2013-3%2017%201%202%204%203%205%201%201-1-1-9-3-10-4-1%202-9%208-9%207%200%209%205%206%2011-2%205-2%208%200%209%204%201%207-8%204-9l-1-4c0-8-10-12-16-6m31%204l-1%208v7h17l-5%205a66%2066%200%2000-19%2037l-2%2010v1l6-7c7-9%2017-15%2017-10%200%202%201%203%203%203s3-1%201-2l-1-3h4c3%200%202%201%200%204l-2%204v3h-17v7c0%206%200%207-2%208-3%202-3%203%200%203%202%200%203%204%201%206l-6%201c-9%200-11-4-7-9l2-5c0-3-8%206-8%2010-1%203-1%203-10%204-8%200-8%200-8%202s1%203%205%203c5%200%206%200%204%205-1%202%201%202%2048%202h49l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2-1-2c-7-1-7-4-3-12%207-13%207-17%200-22-4-3-5-4-5-8l-1-4c-1-1-1%201-1%204%200%205%200%205-2%204l-23%201-3%201v-10l-14-1-13%201m2%208v6h22v-13h-22v7m-31%2027l1%209h12v-18h-13v9m55%2016c0%201-1%202-3%202s-2%201-2%204v3h6c9%200%209%200%208%207l1%208v2l-1%203c0%204%200%204%205%204%207%201%2010-1%207-7v-5l-1-3-3-5-1-3h-1v-2l-3-5c-1-2-3-3-7-4l-5-2v3m-52%207l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%204-2%203-2-2-1-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/f258df3a4f53a83c6fedcb875e36b10b/ee604/blog45.png","srcSet":"/static/f258df3a4f53a83c6fedcb875e36b10b/69585/blog45.png 200w,\n/static/f258df3a4f53a83c6fedcb875e36b10b/497c6/blog45.png 400w,\n/static/f258df3a4f53a83c6fedcb875e36b10b/ee604/blog45.png 800w,\n/static/f258df3a4f53a83c6fedcb875e36b10b/f3583/blog45.png 1200w,\n/static/f258df3a4f53a83c6fedcb875e36b10b/e4d72/blog45.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"graphTraversalTechniques","thumbnail":"thumbnails/blog45.png"}},"staticQueryHashes":["2987289216","63159454"]}