Vertices,edges,undirected and directed graph,cyclic & acyclic graph,degree indegree & outdegree and weights. In an undirected graph, the sum of degrees of all vertices is double the the no. of edges (We consider degree=indegree=outdegree in an undirected graph).

## Graph representation in C++

Let us say there are n nodes and m edges. And adj[i][j] represents there is an edge from node i to j.

There are two ways to represent a graph

#### 1. Adjacency matrix:

**code**

**Time Complexity:**O(m)**** Space Complexity:**O(n*n)**

#### 2. Adjacency List

**code**

Time Complexity:**O(m)** Space Complexity:**O(n*n)**

If there are also weights in edges, we create the adjacency list as ' vector <vector < pair <int,int> > > ' where first value is node(v) and second value represents weight b/w the nodes(u and v).

Since there can be multiple disconnected components in a graph, while graph traversal we have to call BFS/DFS from every unvisited node. To avoid repetition we store information if a node is visited or not by creating a boolean array where is_visited[i]==true represents node 'i' is visited.

```
for(int i=1i<=n;i++)
{
if(!is_visited[i]) BFS(i);
}
```

## BFS

Just after pushing a node in queue,make vis[node]=true;

**code**

Time Complexity:**O(n+e)** ( O(n+e): n time taken for visiting n nodes and e time taken for traversing through adjacent nodes)

Space Complexity:**O(n)** ( O(n) for visiting array and O(n) for queue )

## DFS

**code**

Time Complexity:**O(n+e)** ( O(n+e): n time taken for visiting n nodes and e time taken for traversing through adjacent nodes)

Space Complexity:**O(n)** ( O(n) for visiting array and O(n) for stack space )

## Cycle Detection in Graphs

#### Case 1: Cycle detection in undirected graph using dfs

Since by definition, in an undirected graph, a cycle has at least three nodes,so we will need to store the parent of the node as well. If a node adjacent to a node ( that is not its parent node ) is already visited, the component contains a cycle.

**code**

#### Case 2:Cycle detection in the undirected graph using BFS

**code**

#### Case 3:Cycle detection in the Directed graph using DFS-

Think why color-coding of the graph is required in cycle detection in an undirected graph and why information about the parent of the node is not needed.

**code**

#### Case 4:Cycle detection in the Directed graph using BFS

Assume the directed graph to be acyclic i.e. DAG and find its topological order.If we can do topological sorting i.e. pushing all nodes in queue , the graph is acyclic,other wise it is cyclic.

We increase our count by 1 (starting from 0) each time we push a node in queue. And if final value of count==no. of nodes in graph, the graph is a DAG.

**code**

## Bipartite graph:

A graph will never be bipartite if it has a cycle with odd no. of nodes and if a graph has no cylces with odd no. of nodes, then it must be bipartite.

#### Check whether a graph is bipartite or not using BFS

**code**

#### Check whether a graph is bipartite or not using DFS

**code**

## Topological Sorting:

Only possible in DAG. It is a linear order of vertices such that for every directed edge u-->v, vertex u comes before v in that order. A DAG has at least one node with indegree=0.

#### Topological sorting using BFS

First, calculate indegree of all nodes and store it in vector.Then push all nodes with degree==0 in a queue.Take each node one by one out of queue and for all its child ,decrease their indegree by 1. After this if any child has indegree==0, push them in the queue.

**code**

#### Topological sorting using DFS

Topological order is the order of nodes in decreasing order of finishing time i.e. the node that gets finished at last comes first in order.

**code**

## Shortest path algorithms:

#### Shortest path of all nodes from a node in a unweighted/0-1 weighted graph.

Use BFS for this because BFS visits nodes in a sequential manner. That is nodes at the same level are visited simultaneously. Run BFS and equate dist[node] = 1+dist[parent].

BFS can also be used to find shortest path in 0-1 weighted graph.Instead of using queue,use deque and if weight(node-->child)==0,push at front else at back of deque. BFS can be used to make different binary string using this 0-1 trick. (e.g. in https://www.spoj.com/problems/ONEZERO)

**code**

#### Shortest path of all nodes from a node in a weighted DAG

The weights can be -ve.

Minimum sum to reach a node 'v' i.e. dist[v] is minimum of all (dist[u]+weight(u-->v)),where u is node from all its parents. Similarly, dist[u] is calculated with the help of its parents.We can observe that ultimately we have to start calculating dist[] from source node and in topological order ,we have to visit the nodes

We visit nodes in topological order and relax all its children. In this way, each node is relaxed by all its parents.

**code**

#### Shortest path in +ve weighted graph.

Dijkstra's algorithm is basically an efficient version of breadth-first search on a different graph.

Given a graph G with positive integer edge-weights, one could construct a new unweighted graph H, by replacing each weighted edge with a number of edges equivalent to its weight. So, for example, if you had an edge (u,v) with weight 10 in G, you'd replace this edge with a series of 10 edges between u and v.

Since BFS visits nodes in increasing order of their level (distance from source node) i.e. at any point of time,if a node n1 level is smaller than node n2, minimum distance from n1 will be calculated earlier than n2.

From this, we get an intuition that at any point of time, calculate minimum distance of children of that node which is nearest to the source node. This is a kind of greedy approach because we are relaxing children of that node which is **locally**(at a point of time) nearest to the source.

Since min_priority_queue always stores elements in increasing order,we will use it.We can also use set instead of it.

**code**

**Why Dijkstra doesn't work with -ve weights:**

If all weights are non-negative, adding an edge can never make a path shorter but this is not valid if edge weight is -ve.Dry run a case to explain it further.

Time Complexity:O((E+V)*logV) Space Complexity:O(V)

#### Bellman Ford

Since the negative weighted cycle has sum — infinity, Bellman-Ford works in a directed graph iff the graph has no -ve weighted cycle and works in an undirected graph iff all weights are non -ve.

Relax all edges n-1 times.Why exactly n-1 times.Since in each relaxation,in worst case,only one node will get relaxed and maximum distance of a node from source node is n-1. Consider example 1-->2-->3-->4-->5. First 5 is relaxed,then 4 and so on till 2 ,n-1 times.

**code**

#### Floyd Warshall Algo:

It gives the shortest distance b/w any two nodes.

**Iterative DP code:**

**code**

**Time Complexity:** :O(V*V*V)

## DSU

It is a data structure used to perform the union of disjoint sets efficiently.Each node in the set has parent.

Representative node:Topmost node of a set whose parent is the node itself.

**Naive Implementation:**

**code**

Time complexity:O(n) for make_set and O(d) for find_set() and union_set() where d=maximum depth possible of a node.

To reduce time complexity of find_set() and union_set(),we have to decrease depth.This can be possible if a parent has maximum children possible.

We can do this by doing modification in find_set() and union_set():

For finding representative node a node using find_set(), we will be traversing to its parent, grandfather and so on... . While traversing, we make a representative node,the parent of each node traversed.

While merging two sets, we make the smaller-sized set child of the larger-sized set to ensure minimum depth. (visualize why so, by a diagram). To know the size of the set, we have to maintain an array ,where rnk[i] tells the size of the set treating 'i' as representative node.

**code**

**Time complexity** :O(1)

( Using path compression in find_set() alone reduces time complexity to O (log N)approximately. And time Complexity of find_set() and union_set(), when you use both path compression and union by rank: O( α(N) ) where α(N) = Inverse Ackermann Function which is approximately equal to O(1). )

## Minimum Spanning Tree:

A spanning tree of that graph is a subgraph that is a tree and connects all the vertices together. Thus it has n nodes and n-1 edges. A single graph can have many different spanning trees.

A minimum spanning tree (MST) for a weighted, connected, undirected graph is a spanning tree with sum of the weight of its all edges is less than or equal to the weight of every other spanning tree.

#### Prim's Algo to find MST:

We will start from an empty mstSet and keep adding nodes till no. of nodes in mstSet=n. At any particular time,we will select that node from the children of all nodes already included in MST, whose edge formed will have minimum weight among all possible edges.

This is a greedy approach because we are adding node in a sequence such that it gives least possible weight locally.

**Implementation: **We will take a boolean vector mst to know which node is already included in mst.A int vector 'key',where key[node] stores minimum weight of among all possible edges made by node with its parent. Whenever a new node is inserted, for all children of it,we will update key[child] if weight(node-->child)< key[child].We will insert child in priority queue only if the child has that node as minimum weighted edge among all its parents.We will also store parent[node] to print MST in future.

**Naive Implementation:** Finding minimum of key[node] values among all nodes will take O(n) time therefore, TC=O(n*n).

**Optimal** We will store {key[node],node} of all nodes in a priority queue so that node with minimum key[node] can be popped out in O(logn) time.

**code**

**Time Complexity: **O(nlogn)

#### Kruskal's Algo to find MST:

**Using Greedy+DSU :** Sort all edges in increasing order and keep including the edge only if the edge is not making a cycle. This is greedy approach because we are trying to make globally minimum weight by selecting locally minimum weighted edge.To join to edges ,we use DSU operations,union_set and find_set.

**code**

Time Complexity:O ( E l o g V )

## Finding Strongly Connected Component(SCC)

SCC of a directed graph is a subgraph such that ∃ a path b/w every pair of vertices of the subgraph. (In undirected graph,each component is a SCC.)

Brute Force: Find distance b/w every pair of vertices using floyd warshall algo.Take a node say 1 and push all nodes 'i' where dist[1][i]!=INF in a vector.For nodes pushed take all pairs say (i,j) and delete if the dist[i][j]==INF. Repeat this process. TC:O(N^3).

#### Kosaraju Algo

**Intiution**

If we consider a SCC as one node ,the graph formed will be DAG.

If we reverse the direction of all edges of graph i.e. take transpose of graph ,the SCCs remain the same.

For transpose of a DAG,if we visit nodes in topological order,for one DFS call from main(),only one node will be visited because there will be no outdegree. (source node has become sink node.)

Steps:

Find topological sorting of graph and store it in stack topo.

Find reverse of graph and store it in vector <vector >transpose.

Run DFS in topological order on reversed graph and store in vector <vector > SCCs.

**code**

Time Complexity:O(V+E)