You are on page 1of 2

Module Summary: Graphs

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.

Depth First Search


Is a systematic way of deciding which neighbouring node to visit next.

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.

Breadth First Search


BFS is not a recursive algorithm. In BFS a node visits each of its neighbours before visiting any of
those nodes neighbours. Looking at how many ‘hops’ or steps it takes to visit a node. All nodes that a
are 1 hop away are visited fist, then nodes that are 2 hops from the root node next and so on.

Its time complexity is O(n+m)

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’

For all vertices in hop distance

The shortest path between w and v has length of hops

A shortest path between w and v is given by the path in the BFS

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

There is a path from v to w and

There is a path from w to 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.

You might also like