Bianca Gandolfo PRO
Slide decks for learning JavaScript
Text
@BiancaGando
Graphs are a set of vertices connected by edges
Each item in the graph contains:
@BiancaGando
@BiancaGando
Edges
represent the connection b/t 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
Text
@BiancaGando
Adding an edge
Deleting an edge
Detecting an edge
Finding the neighbors of a vertex
Finding a path between two vertices
@BiancaGando
Text
@BiancaGando
Undirect Graph
Text
@BiancaGando
Directed Graph
@BiancaGando
Weighted Directed Graph
@BiancaGando
Constructor
addNode()
addEdge()
@BiancaGando
Directed Graph
@BiancaGando
Constructor
addNode()
addEdge()
Consider:
How could we represent (un)directed edges?
How could we represent weighted edges?
@BiancaGando
Text
@BiancaGando
@BiancaGando
Why? Find paths, cycles, connectivity and more!
Concepts:
Explored (black), Visited (gray), Undiscovered (white)
@BiancaGando
//Code here
@BiancaGando
@BiancaGando
Mark v as discovered (grey).
For all unvisited (white) neighbors w of v:
Visit vertex w.
Mark v as explored (black).
Text
@BiancaGando
@BiancaGando
@BiancaGando
@BiancaGando
//Code here
@BiancaGando
Create a queue Q.
Mark v as discovered (grey) and enqueue v into Q.
While Q is not empty, perform the following steps:
Dequeue u from Q.
Mark u as discovered (grey).
Enqueue all unvisited (white) neighbors w of u.
Mark u as explored (black).
@BiancaGando
1. BFS Function
- queue
- recursion
2. Tree vs Graph Data Structure
@BiancaGando
O(n)
@BiancaGando
Shortest path finding
Web crawlers
@BiancaGando
@BiancaGando
By Bianca Gandolfo