Professional Documents
Culture Documents
Graphs are comprised of 2 entities, node and vertices, which can be either directed or undirected.
A graph is represented as G = (V, E). We also can use adjacency matrix to represent these graphs.
We need to keep track if a vertex is, unvisited, in progress of visiting all paths, or has no unvisited
paths left. Each vertex is marked as in progress and complete. We visit unvisited neighbours first,
searching the longest path before backtracking once reaching the end. It is not important to travel all
vertices, only to visit all nodes.
This method is useful for finding the shortest path between nodes as everything is labelled as
number of hops away. Can be written as
Start at ‘w’
Bi-partite Graph
A bi-partite graph is a graph where u can separate all the nodes into 2 lists, where there is no
connections among the nodes in one set, only across sets, back and forth.
We can use BFS to quickly identify if a graph is bi-partite or not. As we give each layer an alternating
colour. Once we colour code each layer we check if there are connections between layers of the
same colour and if that never occurs then the graph is bi-partite.
Finding Strongly Connected Components
A graph is strongly connected if there is a always a new path that can be taken/cycle, no dead ends.
They are useful for detecting and finding clusters or communities
For all v, w in V
To determine a SCC we can use DFS to find each path between each src and dst. We pick a root node
to DFS to every other node and write down the nodes. If there are nodes unreachable due to the
direction, we then can start a second DFS in that node to search all the nodes and link it back to the
tree. Joining multiple DFS’s we create a DFS forest, as seen below by the one direction from F,H.