INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
CONCEPT
Graphs are a set of vertices connected by edges
Each item in the graph contains:
1. Stored data -- aka node value
2. Stored references --aka
relationships
aka edges -- to zero or more other nodes
1
4
3
5
2
6
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
PRESENTED BY BIANCA GANDOLFO
VOCABULARY
Edges
represent the connection between 2 vertices
can be directed or undirected
Vertices
nodes in the graph
Path
a sequence of connected vertices
a simple path has no repeated vertices
Cycles
a path that is cyclical
an acyclic graph has no cycles
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
PRESENTED BY BIANCA GANDOLFO
COMMON OPERATIONS
ADDING AN EDGE
DELETING AN EDGE
DETECTING AN EDGE
FINDING THE NEIGHBORS OF A VERTEX
FINDING A PATH BETWEEN TWO VERTICES
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
ADJACENCY MATRIX
Undirected Graph
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
ADJACENCY MATRIX
Directed Graph
ADJACENCY MATRIX
Weighted
Directed Graph
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
PSEUDOCODE
addNode() addEdge()
Constructor
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
ADJACENCY LIST
Directed Graph
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
PSEUDOCODE
addNode() addEdge()
Constructor
CONSIDER:
How could we represent (un)directed edges?
How could we represent weighted edges?
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
EXERCISE TIME!
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
GRAPH TRAVERSING
Why?
Find paths, cycles, connectivity and more!
Concepts:
Explored (black)
Visited (gray)
Undiscovered (white)
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
PSEUDOCODE
//Code here
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
PROCEDURE
1. Mark v as discovered (grey).
2. All unvisited (white) neighbors w of v:
Visit vertex w.
3. Mark v as explored (black).
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
DEPTH-FIRST SEARCH (DFS)
Time
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
EXERCISE TIME!
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
CONCEPT: BFS
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
DIAGRAM: BFS
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
PSEUDOCODE
//Code here
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
PROCEDURE
1. Create a queue Q.
2. Mark v as discovered (grey) and enqueue v into Q.
3. While Q is not empty, perform the following steps:
1. Dequeue u from Q.
2. Mark u as discovered (grey).
3. Enqueue all unvisited (white) neighbors w of u.
4. Mark u as explored (black).
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
INTERFACE: BFS
1. BFS Function
- Queue
- Recursion
2. Tree Graph versus Data Structure
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
TIME COMPLEXITY
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
USE CASES: BFS
1. Shortest Path Finding
2. Web Crawlers
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
EXERCISE TIME!
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
RECAP: CONCEPTS
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
EXERCISE TIME!
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
Introduction to Graphs by Bianca Gandolfo is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
Clean Code in JS by
Bianca Gandolfo is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Based on a work at
https://github.com/ryanmcdermott/clean-code-javascript.