Git Product home page Git Product logo

cs32_graphs_gp's Introduction

Graphs

Day 1

1. What is a graph and how is it represented?

  • What is a Graph?

  • Terminology

    • Directed Vs Undirected

      • Directed (Twitter follow) : Undirected (bi-directional) (Facebook)
      • Undirected Edge is the same as a bi directional edge
    • Cyclic Vs Acyclic

      • cyclic follows a cycle within a graph
      • acyclic does not follow a cycle (to the origin point)
    • Dense Vs Sparse

    • Weighted Vs Unweighted

  • Adjacency Lists & Adjacency Matrices

Lets code an Adjacency List

class Graph:
    """Represent a graph as a dictionary of vertices mapping labels to edges."""
    def __init__(self):
        pass

    def add_vertex(self, vertex_id):
        pass

    def add_edge(self, v1, v2):
        pass

    def get_neighbors(self, vertex_id):
        pass

2. Breadth First & Depth first Traversal

These traversals use the BFS and DFS algorithm respectively

  • BFT Looks at nodes 1 away then nodes 2 away then nodes n away such that n is the next level of children, grand-children etc
  • DFT looks at a neighbor, then the neighbor's neighbor, then the neighbor's neighbor's neighbor etc

Lets take a quick break

After the break we can look at the concept of this traversal and run through the traversing process

3. Partial Traversal example

q = []
visited = {}

Enqueue first vertex:

q = [1]
visited = {}

Dequeue first vertex:

q = []
visited = {}

1

Check if it's been visited (no):

q = []
visited = {}

1

Mark it as visited and enqueue its neighbors:

q = [2]
visited = {1}

Repeat until queue is empty:

q = [2]
visited = {1}

deque item, and repeat process:

q = []
visited = {1, 2, 3, 4, 5, 6, 7}

Lets do some pseudo-code:

def bft(self, starting_vertex_id):
    pass

Lets code up that BFT

def bft(self, starting_vertex_id):
    pass

Lets take a small break

After this break we will look at how we can convert the bft to dft and talk about the difference between bft and bfs

4. What can we do with this to make a dft (Think about the Data Structure used)?

def dft(self, starting_vertex_id):
    pass
s = []
visited = {}

push first vertex:

s = [1]
visited = {}

pop first vertex:

s = []
visited = {}

1

Check if it's been visited (no):

s = []
visited = {}

1

Mark it as visited and push its neighbors:

s = [2]
visited = {1}

Repeat until stack is empty:

s = [2]
visited = {1}

pop item, and repeat process:

s = []
visited = {1, 2, 4, 7, 6, 3, 5}


Lets talk about the bfs using a path

def bfs(self, starting_vertex_id, target_vertex_id):
    # create an empty queue and enqueue the path to the starting vertex id
    # create a set to store visited vertices
    # while queueu not empty
        # dequeue the first path
        # grab the last vertex from the path
        # if vertex is not in visited
            # check if it is the target
                # return the path to the target
            # mark it visited
            # add path to naighbours to back of queue
                # copy the path
                # append the neighbor to the back of it
    # return none
    pass

bfs partial search

q = []
visited = {}

Enqueue path to the first vertex:

q = [[1]]
visited = {}

Dequeue first path :

q = []
visited = {}

[1]

Check if it's been visited (no):

q = []
visited = {1}

[1, 2]

Mark it as visited and enqueue its neighbors:

q = [[1, 2]]
visited = {1}

Repeat until queue is empty:

q = [[1, 2, 3, 5], [1, 2, 4, 6], [1, 2, 4, 7]]
visited = {1, 2, 3, 4}

[1, 2, 4]
4
=> [1, 2, 4]

deque item, and repeat process:

q = []
visited = {1, 2, 3, 4, 5, 6, 7}

Today's Project

let's take a look at the project repo!

cs32_graphs_gp's People

Contributors

decagondev avatar

Watchers

James Cloos 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.