0% found this document useful (0 votes)
9 views6 pages

GRAPH

The document provides algorithms for graph traversal using BFS and DFS to find the number of provinces in an undirected graph. It also discusses Dijkstra's algorithm for finding the shortest path and a modified version to determine the maximum probability path between two nodes in a weighted graph. The time and space complexities for each algorithm are noted, emphasizing their efficiency.

Uploaded by

Chaitanya Hange
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

GRAPH

The document provides algorithms for graph traversal using BFS and DFS to find the number of provinces in an undirected graph. It also discusses Dijkstra's algorithm for finding the shortest path and a modified version to determine the maximum probability path between two nodes in a weighted graph. The time and space complexities for each algorithm are noted, emphasizing their efficiency.

Uploaded by

Chaitanya Hange
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

GRAPH

BFS:
Same as level order traversal of tree
Code:
int numProvinces(vector<vector<int>>& adj, int V) {
vector<int> visited(V, 0); // Initialize visited array with 0

int ans = 0;
queue<int> q;

for (int i = 0; i < V; i++) {


if (visited[i] == 0) {
ans++;
q.push(i);
visited[i] = 1;

while (!q.empty()) {
int curr = q.front();
q.pop();

for (int neighbor : adj[curr]) {


if (visited[neighbor] == 0) {
q.push(neighbor);
visited[neighbor] = 1;
}
}
}
}
}

return ans;
}

DFS:
Same as pre order traversal of tree
Code:
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
// Function to return a list containing the DFS traversal of the graph.
vector<int>ans;
void dfs(int x,vector<int> adj[],vector<int> &visited,int V){
visited[x]=1;
ans.push_back(x);
for(int i=0;i<adj[x].size();i++){
if(visited[adj[x][i]]!=1){
dfs(adj[x][i],adj,visited,V);
}

}
vector<int> dfsOfGraph(int V, vector<int> adj[]) {
vector<int >visited(V,0);
dfs(0,adj,visited,V);
return ans;
}
};

Problem 1]:

Given an undirected graph with V vertices. We say two vertices u


and v belong to a single province if there is a path from u to v or v
to u. Your task is to find the number of provinces.

Note: A province is a group of directly or indirectly


connected cities and no other cities outside of the group.
Approach:
Apply bfs or dfs
void dfs(int index, const vector<vector<int>>& adj, int visited[]) {
visited[index] = 1;
for (int i = 0; i < adj[index].size(); i++) {
if (visited[i] == 0 && adj[index][i] == 1) {
dfs(i, adj, visited);
}
}
}

int numProvinces(vector<vector<int>> adj, int V) {


int* visited = new int[V];
for (int i = 0; i < V; i++) {
visited[i] = 0;
}
int ans = 0;
for (int i = 0; i < adj.size(); i++) {
if (visited[i] != 1) {
ans++;
dfs(i, adj, visited);
}
}

delete[] visited;
return ans;
}

DIJSKTRA ALGORITHM :

To find shortest path between given node and any vertex


Using min priority queue it can be implemented and like bfs traverse each node and
check for minegdedistane if found then update it in ans array and push pair of
mindistance and node in queue
**not works with negedge graph it falls in infinite loop
vector <int> dijkstra(int V, vector<vector<int>> adj[], int S)
{
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;

vector<int >dis(V,INT_MAX);
dis[S]=0;
pq.push({0,S});
while(!pq.empty()){
int edgedistance=pq.top().first;
int node=pq.top().second;
pq.pop();

for(int j=0;j<adj[node].size();j++){
int adjdis=adj[node][j][1];
int adjnode=adj[node][j][0];
if(edgedistance+adjdis<dis[adjnode]){
pq.push({adjdis+edgedistance,adjnode});
dis[adjnode]=adjdis+edgedistance;
}
}
}
return dis;
}

TC: O((V+E)LOGV) \\ SC:O(V)

Problem Statement: You are given an undirected weighted graph of n nodes (0-
indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge
connecting the nodes a and b with a probability of success of traversing that
edge succProb[i].

Given two nodes start and end, find the path with the maximum probability of success
to go from start to end and return its success probability.

If there is no path from start to end, return 0. Your answer will be accepted if it differs
from the correct answer by at most 1e-5.

Approach: use dijkraj algorithm with some changes if product of


probability is grater than stored push in priority q
Code:

vector<vector<pair<int, double>>> adj(n);


for (int i = 0; i < edges.size(); i++) {
int u = edges[i][0];
int v = edges[i][1];
adj[u].push_back({v, succProb[i]});
adj[v].push_back({u, succProb[i]});
}

vector<double> dis(n, 0.00);


dis[start] = 1.00;

priority_queue<pair<double, int>> pq;


pq.push({1.0, start});

while (!pq.empty()) {
pair<double, int> p = pq.top();
pq.pop();

int node = p.second;


double prob = p.first;

if (prob < dis[node])


continue;

for (auto& neighbor : adj[node]) {


int adjNode = neighbor.first;
double adjProb = neighbor.second;

double newProb = prob * adjProb;


if (newProb > dis[adjNode]) {
pq.push({newProb, adjNode});
dis[adjNode] = newProb;
}
}
}

return dis[end];
}

TC: O(V+E)LOG(V) ///// SC: O(V + E)

You might also like