Data Structure and Algorithm 5 | Graph Algorithms

0_QXrDY4nVmhZ1umbX

1. Graph Representation

(1) Adjacency Matrix

The adjacency matrix with shape is a symmetric matrix that has {0, 1} as its values, where 1 means two nodes are connected.

(2) Adjacency List

Adjacency list is a list of length stores the list of edge information.

(3) Connected Nodes

A node in the graph is defined by,

This is the common implementation due to nice mapping to objects.

(4) Node with Labelled Edges

2. Graph Algorithms

(1) Recall: DFS Search

Time complexity .

(2) Reachable Nodes

Start from a node, then get a set of nodes it can reach by,

(3) Detecting Cycles

(4) Find First Path

(5) Find all Path

(7) BFS Search

(8) Shortest Path

(9) Depth/Distance

(10) Find Neighbour within k Distance

(11) DFS Based Topological Sort

Here visited set is added not because we are caring about the cycles. It means that we don't take the nodes we have visited into the account because these nodes have already beem sorted.

Here it requires a post order DFS as we have written in the searching part.