Skip to content

All easy gfg solution added #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ You don't need to read input or print anything. Your task is to complete the fun

### Constraints


- $1 ≤ N ≤ 10^5$
- $-10^3 ≤ arr[i] ≤ 10^3$

Expand Down
13 changes: 7 additions & 6 deletions dsa-solutions/gfg-solutions/0024-minimum-indexed-character.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ There are no characters that are common in "patt" and "str".
### Your Task
You only need to complete the function `minIndexChar()` that returns the index of the answer in `str` or returns `-1` if no character of `patt` is present in `str`.

**Expected Time Complexity:** O(N)
**Expected Auxiliary Space:** O(Number of distinct characters)
**Expected Time Complexity:** $O(N)$
**Expected Auxiliary Space:** $O(Number of distinct characters)$

### Constraints
- 1 ≤ |str|,|patt| ≤ 10^5
- 'a' ≤ str[i], patt[i] ≤ 'z'

- $1 ≤ |str|,|patt| ≤ 10^5$
- $'a' ≤ str[i], patt[i] ≤ 'z'$

## Solution

Expand Down Expand Up @@ -169,8 +170,8 @@ class Solution {

### Complexity Analysis

- **Time Complexity:** O(N), where N is the length of the string `str`. We iterate through each character in `patt` and use the `find` or `indexOf` method, which runs in O(N) time.
- **Space Complexity:** O(1), as we only use a constant amount of extra space for variables.
- **Time Complexity:** $O(N)$, where N is the length of the string `str`. We iterate through each character in `patt` and use the `find` or `indexOf` method, which runs in $O(N)$ time.
- **Space Complexity:** $O(1)$, as we only use a constant amount of extra space for variables.

---

Expand Down
222 changes: 222 additions & 0 deletions dsa-solutions/gfg-solutions/Easy problems/bfs-of-graph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
---
id: bfs-of-graph
title: BFS of Graph
sidebar_label: 8 BFS of Graph
tags:
- Graph
- BFS
- Python
- Java
- C++
- JavaScript
- TypeScript
description: "This document provides solutions to the problem of performing a Breadth First Search (BFS) traversal of a directed graph in various programming languages."
---

## Problem

Given a directed graph, perform a Breadth First Traversal starting from the 0th vertex. One can move from node `u` to node `v` only if there's an edge from `u` to `v`. Find the BFS traversal of the graph starting from the 0th vertex, from left to right according to the input graph. Only consider nodes that are directly or indirectly connected to node `0`.

### Examples

**Example 1:**

```
Input: V = 5, E = 4, adj = {{1,2,3},{},{4},{},{}}
Output: 0 1 2 3 4
Explanation:
0 is connected to 1, 2, 3.
2 is connected to 4.
Starting from 0, it will go to 1 then 2 then 3. After this, 2 to 4, thus BFS will be 0 1 2 3 4.
```

**Example 2:**

```
Input: V = 3, E = 2, adj = {{1,2},{},{}}
Output: 0 1 2
Explanation:
0 is connected to 1, 2.
Starting from 0, it will go to 1 then 2, thus BFS will be 0 1 2.
```

### Your Task

You don't need to read input or print anything. Your task is to complete the function `bfsOfGraph()` which takes the integer `V` denoting the number of vertices and adjacency list as input parameters and returns a list containing the BFS traversal of the graph starting from the 0th vertex from left to right.

**Expected Time Complexity:** $O(V + E)$
**Expected Auxiliary Space:** $O(V)$

**Constraints**
- $1 ≤ V, E ≤ 10^4$

## Solution

### Intuition & Approach

Breadth First Search (BFS) is a method to traverse a graph level by level, starting from a given node. We use a queue data structure to facilitate this traversal. The main steps are:

1. Create a boolean list `visited` to mark all vertices as not visited.
2. Initialize the traversal from the starting vertex (0th vertex).
3. Use a queue to explore nodes level by level, marking each node as visited once it is processed.
4. For each node, explore its adjacent vertices that have not been visited yet, marking them as visited and adding them to the queue.
5. Continue this process until the queue is empty.

### Implementation

<Tabs>
<TabItem value="python" label="Python">

```python
from collections import deque

class Solution:

def bfsOfGraph(self, V, adj):
visited = [False] * V
queue = deque([0])
visited[0] = True
result = []

while queue:
node = queue.popleft()
result.append(node)
for neighbor in adj[node]:
if not visited[neighbor]:
queue.append(neighbor)
visited[neighbor] = True
return result
```

</TabItem>
<TabItem value="java" label="Java">

```java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

class Solution {
public ArrayList<Integer> bfsOfGraph(int V, ArrayList<ArrayList<Integer>> adj) {
ArrayList<Integer> traversal = new ArrayList<>();
Queue<Integer> queue = new LinkedList<>();
boolean[] visited = new boolean[V];
queue.add(0);
visited[0] = true;

while (!queue.isEmpty()) {
int node = queue.poll();
traversal.add(node);
for (int neighbor : adj.get(node)) {
if (!visited[neighbor]) {
queue.add(neighbor);
visited[neighbor] = true;
}
}
}
return traversal;
}
}
```

</TabItem>
<TabItem value="cpp" label="C++">

```cpp
#include <vector>
#include <queue>

class Solution {
public:
std::vector<int> bfsOfGraph(int V, std::vector<int> adj[]) {
std::vector<int> result;
std::vector<bool> visited(V, false);
std::queue<int> q;

q.push(0);
visited[0] = true;

while (!q.empty()) {
int node = q.front();
q.pop();
result.push_back(node);
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
q.push(neighbor);
visited[neighbor] = true;
}
}
}
return result;
}
};
```

</TabItem>
<TabItem value="javascript" label="JavaScript">

```javascript
class Solution {
bfsOfGraph(V, adj) {
let visited = new Array(V).fill(false);
let queue = [0];
visited[0] = true;
let result = [];

while (queue.length > 0) {
let node = queue.shift();
result.push(node);
for (let neighbor of adj[node]) {
if (!visited[neighbor]) {
queue.push(neighbor);
visited[neighbor] = true;
}
}
}
return result;
}
}
```

</TabItem>
<TabItem value="typescript" label="TypeScript">

```typescript
class Solution {
bfsOfGraph(V: number, adj: number[][]): number[] {
let visited = new Array(V).fill(false);
let queue: number[] = [0];
visited[0] = true;
let result: number[] = [];

while (queue.length > 0) {
let node = queue.shift() as number;
result.push(node);
for (let neighbor of adj[node]) {
if (!visited[neighbor]) {
queue.push(neighbor);
visited[neighbor] = true;
}
}
}
return result;
}
}
```

</TabItem>
</Tabs>

---

## Complexity analysis

The provided solutions efficiently perform a Breadth First Search (BFS) traversal of a directed graph. By starting from the 0th vertex and using a queue to manage the traversal, the algorithms ensure that all reachable vertices are visited in the correct order. The solutions operate in $O(V + E)$ time and use $O(V)$ space complexity, where V and E are the numbers of vertices and edges in the graph, respectively.

**Time Complexity:** $O(V + E)$
**Auxiliary Space:** $O(V)$

## References

- **GeeksforGeeks Problem:** [BFS of graph](https://www.geeksforgeeks.org/problems/bfs-traversal-of-graph/0)
- **Author GeeksforGeeks Profile:** [GeeksforGeeks](https://www.geeksforgeeks.org/user/GeeksforGeeks/)
Loading
Loading