graphs Flashcards
adjacency matrix facts
Given two vertices u and v: find out whether u and v are adjacent –> O(1)
Given a vertex u: enumerate all neighbors of u –> O(n)
For all vertices: enumerate all neighbors of each vertex O(n^2)
memory space is O(n^2)
adjacency list facts
Given two vertices u and v: find out whether u and v are adjacent –> degree of node O(n)
Given a vertex u: enumerate all neighbors of u –> degree of node O(n)
For all vertices: enumerate all neighbors of each vertex summations of all node degree O(m)
memory space is O(m)
better for sparse graphs
when is a graph sparse?
a graph is spare is the number of edges is less than the number of nodes squared
dijkstra’s algorithm
finding shortest path from node to node, traversing
time complexity: O(n log n + m log n(
bellman-ford
shortest path, the one with going through each node and seeing if there can be a new shortest path by relaxing
time complexity O(m * n)
kruskal’s algorithm
minimum spanning tree found by the next shortest absolute node that doesn’t create a cycle
prim’s algorithm
minimum spanning tree found by the next shortest traversal
O(n^2) for dense graph
O(m log n) for sparse graph