forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '7oSkaaa:main' into main
- Loading branch information
Showing
9 changed files
with
418 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...ces to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Lama Salah).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Author : Lama Salah | ||
|
||
class Solution { | ||
public: | ||
int par[100005]; // Initialize 'par' array of size (1e5 + 5) to keep track of the parent of each node. | ||
|
||
// Find function to get and update the root parent of a node. | ||
int find(int node){ | ||
// If the node is its own parent, return the node. | ||
// Otherwise, recursively find the parent and update the parent of the current node. | ||
return par[node] == node ? node : par[node] = find(par[node]); | ||
} | ||
|
||
vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) { | ||
vector <int> ans; // Vector to store the smallest set of vertices. | ||
set <int> unique; // Set to store unique root parents. | ||
|
||
// Step 1: Initialize parent array for each node. | ||
for (int i = 0; i < n; i++) { | ||
par[i] = i; | ||
} | ||
|
||
// Step 2: Iterate through the 'edges' vector and for each edge, | ||
// set the parent of the second node (e[1]) as the first node (e[0]). | ||
for (auto& e : edges) { | ||
par[e[1]] = e[0]; | ||
} | ||
|
||
// Step 3: Find the root parent of each node and insert into the set. | ||
for (int i = 0; i < n; i++) { | ||
unique.insert(find(i)); | ||
} | ||
|
||
// Step 4: Append the root parents to the result vector. | ||
for (auto& i : unique) { | ||
ans.emplace_back(i); | ||
} | ||
|
||
// Return the minimum number of vertices to reach all nodes. | ||
return ans; | ||
} | ||
}; |
25 changes: 25 additions & 0 deletions
25
...to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Noura Algohary).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Author: Noura Algohary | ||
class Solution { | ||
public: | ||
vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) { | ||
// a vector to show the nodes that have incoming edges | ||
vector<int>reached(n, 0); | ||
// nodes with zero incoming edges | ||
vector<int>solution; | ||
|
||
for(auto edge:edges) | ||
{ | ||
// count how many times the node is reached | ||
reached[edge[1]]++; | ||
} | ||
|
||
for(int node = 0;node<n;node++) | ||
{ | ||
// is the node have a count of 0 incoming edges | ||
if(reached[node] == 0) | ||
solution.push_back(node); | ||
} | ||
|
||
return solution; | ||
} | ||
}; |
18 changes: 18 additions & 0 deletions
18
... to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Noura Algohary).py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Author: Noura Algohary | ||
class Solution: | ||
def findSmallestSetOfVertices(self, n: int, edges: List[List[int]]) -> List[int]: | ||
# a list to show the nodes that have incoming edges | ||
reached = [0 for i in range(n)] | ||
# nodes with zero incoming edges | ||
solution = [] | ||
|
||
for edge in edges: | ||
# count how many times the node is reached | ||
reached[edge[1]]+=1 | ||
|
||
for node in range(n): | ||
# is the node have a count of 0 incoming edges | ||
if reached[node] ==0: | ||
solution.append(node) | ||
|
||
return solution |
20 changes: 20 additions & 0 deletions
20
...s to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Osama Ayman).java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Author: Osama Ayman | ||
// Time: O(N + E) | ||
// Space: O(N) | ||
class Solution { | ||
public List<Integer> findSmallestSetOfVertices(int n, List<List<Integer>> edges) { | ||
boolean[] incomingEdge = new boolean[n]; | ||
for(List<Integer> e: edges){ | ||
incomingEdge[e.get(1)] = true; | ||
} | ||
List<Integer> res = new ArrayList<>(); | ||
// Any node that has no incoming edge can not be visited untill we start from it | ||
for(int i=0; i<n; i++){ | ||
if(!incomingEdge[i]){ | ||
// add node to the result list | ||
res.add(i); | ||
} | ||
} | ||
return res; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Ahmed Hossam).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Author: Ahmed Hossam | ||
|
||
class Solution { | ||
public: | ||
|
||
vector < int > colour; | ||
|
||
// Function to check if a graph is bipartite | ||
bool is_Bipartite(int u, vector < vector < int > >& adj) { | ||
// Iterate over the adjacent vertices of u | ||
for (auto v : adj[u]) { | ||
// If v has the same color as u, the graph is not bipartite | ||
if (colour[v] == colour[u]) | ||
return false; | ||
// If v is uncolored, assign a different color to it | ||
else if (colour[v] == 0) { | ||
colour[v] = -colour[u]; | ||
// Recursively check if the subgraph starting from v is bipartite | ||
if (!is_Bipartite(v, adj)) | ||
return false; | ||
} | ||
} | ||
// All adjacent vertices have been processed and no conflict was found | ||
return true; | ||
} | ||
|
||
bool isBipartite(vector < vector < int > >& graph) { | ||
int n = graph.size(); | ||
colour = vector < int > (n); | ||
|
||
bool isBip = true; | ||
// Process each vertex in the graph | ||
for (int u = 0; u < n; u++) { | ||
// If the vertex is uncolored, assign color 1 to it | ||
if (!colour[u]) { | ||
colour[u] = 1; | ||
// Check if the subgraph starting from u is bipartite | ||
isBip &= is_Bipartite(u, graph); | ||
} | ||
} | ||
|
||
// Return whether the graph is bipartite or not | ||
return isBip; | ||
} | ||
|
||
}; |
49 changes: 49 additions & 0 deletions
49
05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Lama Salah).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Author: Lama Salah | ||
|
||
class Solution { | ||
public: | ||
vector<vector<int>> adj; // Adjacency list to represent the graph. | ||
vector<int> vis; // Array to keep track of node colors. | ||
|
||
// Recursive function to check if the graph is not bipartite. | ||
bool isNotBipartite(int node, int color) { | ||
vis[node] = color; // Assign the current node the specified color (0 or 1). | ||
|
||
bool ans = false; // Variable to store the result. | ||
|
||
for (auto& i : adj[node]) { | ||
if (vis[i] == -1) { | ||
// If the neighbor hasn't been visited, recursively call the function with the opposite color. | ||
ans |= isNotBipartite(i, color ^ 1); | ||
} else if ((color ^ 1) != vis[i]) { | ||
// If the neighbor has been visited and has the same color as the current node, the graph is not bipartite. | ||
return true; | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
bool isBipartite(vector<vector<int>>& graph) { | ||
adj.assign(graph.size(), vector<int>()); // Initialize the adjacency list. | ||
vis.assign(graph.size(), -1); // Initialize the vis array. | ||
|
||
// Build the adjacency list based on the given graph. | ||
for (int i = 0; i < graph.size(); i++) { | ||
for (auto& j : graph[i]) { | ||
adj[i].push_back(j); | ||
adj[j].push_back(i); | ||
} | ||
} | ||
|
||
// Iterate over each node in the graph. | ||
for (int i = 0; i < graph.size(); i++) { | ||
if (vis[i] == -1 && isNotBipartite(i, 0)) { | ||
// If the node hasn't been visited and the graph is not bipartite, return false. | ||
return false; | ||
} | ||
} | ||
|
||
return true; // The graph is bipartite. | ||
} | ||
}; |
58 changes: 58 additions & 0 deletions
58
05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Mahmoud aboelsoud).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// author: Mahmoud Aboelsoud | ||
|
||
class Solution { | ||
public: | ||
// we just need to check if the graph is bipartite or not | ||
// we can do that by coloring the nodes with 2 colors and check if there is a node with 2 colors | ||
// if there is a node with 2 colors then the graph is not bipartite | ||
// else the graph is bipartite | ||
|
||
|
||
// graph: the adjacency list of the graph | ||
vector<vector<int>> graph; | ||
// cols: the color of each node | ||
vector<int> cols; | ||
// n: the number of nodes in the graph | ||
int n; | ||
|
||
// a dfs function to color the nodes | ||
bool dfs(int node, int col){ | ||
// color the node with col | ||
cols[node] = col; | ||
|
||
// iterate over the neighbours of the node | ||
for(auto&i: graph[node]){ | ||
// if the neighbour is not colored then color it with the opposite color of the node | ||
if(cols[i] == -1){ | ||
// if the coloring failed after coloring the neighbour then the graph is not bipartite | ||
if(!dfs(i, !col)) return false; | ||
// if the neighbour is colored then check if it has the same color of the node | ||
}else{ | ||
// if the neighbour has the same color of the node then the graph is not bipartite | ||
if(cols[i] == col) return false; | ||
} | ||
} | ||
// if we reached here then the graph is bipartite | ||
return true; | ||
} | ||
|
||
|
||
bool isBipartite(vector<vector<int>>& graph) { | ||
// initialize the graph and the cols array and the number of nodes | ||
this -> graph = graph; | ||
n = graph.size(); | ||
cols.assign(n, -1); | ||
|
||
// the graph may not be connected so we need to check each component | ||
for(int i = 0; i < n; i++){ | ||
// if the node is not colored then color it with 0 | ||
if(cols[i] == -1){ | ||
// if the coloring failed then the graph is not bipartite | ||
if(!dfs(i, 0)) return false; | ||
} | ||
} | ||
|
||
// if we reached here then the graph is bipartite | ||
return true; | ||
} | ||
}; |
49 changes: 49 additions & 0 deletions
49
05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Omar Sanad).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// author : Omar Sanad | ||
|
||
class Solution { | ||
public: | ||
// declare an array to store the color of each node | ||
int color[101]; | ||
|
||
// an adjacency list, where each node points to its neighbours | ||
vector<vector<int>> adj; | ||
|
||
// a dfs function to check whether a graph is Bipartite or not... | ||
// it keeps track of the current node, and the color of the parent node | ||
bool isBi(int node, int prevColor) { | ||
|
||
// if this node already has a color | ||
if (color[node]) | ||
// then if the color is the same as its parent (its neighbour), the graph is not Bipartite | ||
// else if the color of this node is different from its parent (its neighbour), till now graph not Bipartite | ||
return color[node] != prevColor; | ||
|
||
// declare a variable that checks whether all the neighbours colors can be different from the current node | ||
bool ok = true; | ||
|
||
// assign a color for the current node, give it a color that's different from the previous color | ||
color[node] = 3 - prevColor; | ||
|
||
// iterate over all neighbours of the current node, and check if we can assign them a color different from the current node. | ||
for (auto &child : adj[node]) | ||
ok &= isBi(child, color[node]); | ||
|
||
// return the status till now (is it Bipartite or not)? | ||
return ok; | ||
} | ||
|
||
bool isBipartite(vector<vector<int>>& graph) { | ||
this->adj = graph; | ||
|
||
// declare a variable that checks for each component if it is Bipartite or not | ||
bool ok = true; | ||
|
||
// iterate over each non-visited node (in other words, each connected component), and check if the component is Bipartite or not | ||
for (int i = 0; i < graph.size(); i++) | ||
if (not color[i]) | ||
ok &= isBi(i, 1); | ||
|
||
// return the answer to the problem | ||
return ok; | ||
} | ||
}; |
Oops, something went wrong.