graphs Flashcards
Three different Types of Graphps
1) Edge Lists
2) Adjacency Matrixes
3) Adjacency Lists
Two most common ways to use graph data structures
1) Breadth First Search
2) Depth First Search
Edge List B-O
O(n)
Data structure for Edge List
Array
[0,1]
[1,2]
Adjacency Matrices B-O
O(1)
Data structure Adjacency Matrix
connect 4 type matrix (maybe battleship)
Problem with Adjacency Matrix
Takes up a lot of space
Problem with Edge List
slower
Data structure Adjacency Lists
Write each of it’s neighbors as a list in an array
0 -> [1,4] // 0 is connected to 1 and 4
1 -> [2] // only one neighbor
(an array of arrays)
Adjacency Lists 3 Advantages
1) Fast lookup
2) easy to find neighbors
3) Not as large as Adjacency Matrices
Edge List (unidirectional)
Two array lists
[0,2] [2,0]
Adjacency Matrix (unidirectional)
Flipped gride (both sides)
Adjacency Lists (unidirectional)
Just add an other item to array
0 -> [1,4,5] // 0 is connected to 1 and 4
1 -> [2,1] // only one neighbor
Breadth First Search (what is it)
start in the middle and move outward
Breadth First Search what 3 things is it used for?
1) Social Graphs
2) Recommendations
3) Nearest Neighbors
Depth First Search (what is it)
Starts with a node and drives deep and ignores nodes around it. Following it till it hit the end
Depth First Search what 3 things is it used for?
1) Path Fionding
2) Unique solutions (path remains)
3) Cycle detection in graph
Breadth First Search uses what structure?
Queue (first in first out) FIFO
Depth First Search uses what structure?
Stack (First in last out) LIFO
interview question: Breadth First (uses)
Nearest Neighbor (Social Networks, FB, Linkedin) nearby peers in games
interview question: Depth First (uses)
Better way to find things faraway (chess game, paths
interview question: Breadth First (tech)
Queue (first in first out)
interview question: Depth First (tech)
Stack (in in last out)