Giter Club home page Giter Club logo

depth-first-search-atlanta-web-042219's Introduction

Depth First Search

There are multiple techniques for exploring the connectivity of a graph. In this lesson, we will discuss depth first search as one of these techniques.

Depth First Search

Depth first search is one mechanism for determining if a graph is connected. Depth first search occurs similarly to how a child may explore a corn maze. Upon reaching a certain distance, just push on further until you hit a dead end. In other words, dive down one path, then dive down another path until all of the paths are explored.

Our approach

Let's use our map of subway stops. Here we have a graph representing portions of the Sixth avenue line, the Broadway line, and the Lexington line.

With depth first search, we start at a given vertex and explore that vertex by visiting each of the adjacent vertices. Every time we visit a vertex we place it on a stack (whatever that is), and the next vertex we explore is the top of the stack. This results in us visit and explore one line fully like the 6th avenue line, and then backtracking to explore the Broadway and Lexington lines. Let's see how.

We randomly decide to start by visiting from 34th & 6th. Now that we visit the vertex, we place it on the top of the stack.

Note: The way that a stack operates is last in first out such that the last element inserted in is the first element removed from a stack. For example, think of stacking cafeteria trays on a trashcan: the last one in is on top, and therefore removed first.

stack
34th & 6th

We remove 34th & 6th from our stack, and explore the vertex by visiting all of the adjacent vertices. 34th and 6th has two adjacent vertices, 28th and Broadway and 23rd and 6th.

So now take a look at our stack of visited vertices listed in the diagram above. It has 23rd and 6th and 28th and Broadway. We take the vertex from the top of the stack, 23rd & 6th, and explore that vertex. This means we look for adjacent unvisited nodes and find one in 14th & 6th. We add 14th and 6th to the top of the stack and will explore that next.

Ok, so now from the top of the stack, we choose 14th & 6th. From 14th & 6th there are no unvisited adjacent nodes, so we remove 14th and 6th from top of the stack, and will explore the next vertex on our stack at at 28th & Broadway. Note that this brings to explore the second path down from our root node.

From here, you may be able to fill in the rest of the procedure. We continue to explore the vertex at the top of our stack, which leads us to visit another node. And then we explore the next vertex at the top of our stack. We continue to do this we run out of vertexes to visit, at which point our stack will become empty and the algorithm will terminate.

Let's quickly go through these final steps.

Now that our stack looks like the following:

top of stack
28th & Broadway

We remove 28th & Broadway from the top of the stack, and move onto 23rd & Broadway as it is the next adjacent unvisited node.

top of stack
23rd & Broadway

From 23rd and Broadway our next unvisited adjacent node is 14th & Lex. We add that to the top of the stack.

top of stack
14th & Lex

We remove 14th & Lex from the top of the stack, explore it, and find 23rd and Lex.

top of stack
23rd & Lex

We remove 23rd and Lex, explore it only to find that there are no adjacent unvisited nodes, and so we terminate the algorithm.

Summary

How would you summarize this procedure? How would you translate it into code? We'll ask you to translate this into code, so take a good look.

depth-first-search-atlanta-web-042219's People

Contributors

jeffkatzy avatar maxwellbenton avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.