Skip to content

Instantly share code, notes, and snippets.

@sudipp
Created April 12, 2019 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudipp/a23c573fad40a16bbdfee6482a2a40cf to your computer and use it in GitHub Desktop.
Save sudipp/a23c573fad40a16bbdfee6482a2a40cf to your computer and use it in GitHub Desktop.
public class Solution {
public int[] FindRedundantDirectedConnection(int[][] edges)
{
/*
Step 1. Build In and Out Degree map, detect if any any vertex with Multiple parents.
Step 2. If we don't find vertex 'A' with multiple parents
2.1) Start at 'root' and detect any cycle with DFS, return the edge causing the cycle
Step 3. If we find vertex 'A' with multiple parents
3.1) if out degree at vertex 'A' is > 0, i.e there is a edge going out of the 'A' vertex
3.1.1) Start at 'A' and detect any cycle with DFS, return the edge causing the cycle
3.2) if there is NO outgoing edge from vertex 'A'
3.2.1) Return the LAST edge that is ending at 'A' (i.e 'A' last parent)
*/
var vertexInDegree = new List<int>[edges.Length + 1];
var vertexOutDegree = new List<int>[edges.Length + 1];
for (int i = 0; i < edges.Length + 1; i++) vertexInDegree[i] = new List<int>();
for (int i = 0; i < edges.Length + 1; i++) vertexOutDegree[i] = new List<int>();
bool[] visited = new bool[edges.Length + 1]; // as vertex # starts with 1
int vertexWithMultipleParents = -1;
foreach (var arr in edges)
{
vertexInDegree[arr[1]].Add(arr[0]);
vertexOutDegree[arr[0]].Add(arr[1]);
if (vertexInDegree[arr[1]].Count > 1)
vertexWithMultipleParents = arr[1];
}
if (vertexWithMultipleParents != -1)
{
//detect if there is out edges from Vertex with multiple parents
//if yes, defect this edge cause a cycle, if yes, remove the first edge
//else remove the end edge
//start from vertex with multiple parents ***********
int root = vertexWithMultipleParents;
if (vertexOutDegree[root].Count >0) //if there is a out edge
{
Stack<KeyValuePair<int, int>> stack = new Stack<KeyValuePair<int, int>>();
stack.Push(new KeyValuePair<int, int>(root, -1));
while (stack.Any())
{
var node = stack.Pop();
if (!visited[node.Key])
{
visited[node.Key] = true;
foreach (int c in vertexOutDegree[node.Key])
stack.Push(new KeyValuePair<int, int>(c, node.Key));
}
else
{
//cycle detected
return new int[] { node.Value, node.Key };
}
}
return new int[] { vertexInDegree[root][1], root };
}
else //doesnt have any out degree, return the last one
{
return new int[] { vertexInDegree[root][1], root };
}
}
else
{
//if multple parent vertex NOT found,
//then search for cycle, if found, then remove the edge causing the cycle
//first edge's as start
int root = edges[0][0];
Stack<KeyValuePair<int, int>> stack = new Stack<KeyValuePair<int, int>>();
stack.Push(new KeyValuePair<int, int>(root, -1));
while (stack.Any())
{
var node = stack.Pop();
if (!visited[node.Key])
{
visited[node.Key] = true;
foreach (int c in vertexOutDegree[node.Key])
stack.Push(new KeyValuePair<int, int>(c, node.Key));
}
else
{
//cycle detected
return new int[] { node.Value, node.Key};
}
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment