public static int[] _2Satisfiability(DirectedGraph g) { MetaGraph m = new MetaGraph(g); //return m.stack.ToArray(); //m.id.Length ~ m.stack.Count //m.id[2 * i + 1] ~ m.stack.ElementAt() for (int i = 0; i < m.id.Length / 2; i++) //for (int i = 0; i < m.stack.Count / 2; i++) { // if not solving if (m.id[2 * i] == m.id[2 * i + 1]) //if (m.stack.ElementAt(2 * i) == m.stack.ElementAt(2 * i + 1)) { return new int[1] { 0 }; } } int[] res = new int[m.id.Length / 2 + 1]; //int[] res = new int[m.stack.Count / 2 + 1]; res[0] = 1; // see Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006, page 102, task 3.28 for (int i = m.id.Length; i > 0; i--) //for (int i = m.stack.Count; i > 0; i--) { for (int v = 0; v < m.id.Length; v++) //for (int v = 0; v < m.stack.Count; v++) { if (m.id[v] != i) //if (m.stack.ElementAt(v) != i) { continue; } if (res[v / 2 + 1] != 0) { continue; } int r; if (v % 2 == 0) { r = (v + 2) / (+2); } else { r = (v + 1) / (-2); } res[v / 2 + 1] = r; } } return res; }
public _2Satisfiability(string file) { string path = Utils.GetFullPathFile(file); try { using (StreamReader reader = new StreamReader(path)) { string line; int i = -1; string[] param; int CountClauses = 0; int CountVertices; line = reader.ReadLine(); int CountConjunctions = int.Parse(line); g = new DirectedGraph[CountConjunctions]; while ((line = reader.ReadLine()) != null) { if (line == "") { continue; } if (CountClauses == 0) { param = line.Split(' '); CountVertices = 2 * int.Parse(param[0]); CountClauses = int.Parse(param[1]); i++; g[i] = new DirectedGraph(CountVertices); continue; } param = line.Split(' '); int v0 = Int32.Parse(param[0]); int v1 = Int32.Parse(param[1]); g[i].AddEdge(Index(-v0), Index(v1)); g[i].AddEdge(Index(-v1), Index(v0)); CountClauses--; } } } catch (Exception e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } }
public DirectedGraphs(string file) { string path = Utils.GetFullPathFile(file); try { using (StreamReader reader = new StreamReader(path)) { string line; int i = -1; string[] param; int CountEdges = 0; int CountVertices; line = reader.ReadLine(); int CountGraphs = int.Parse(line); g = new DirectedGraph[CountGraphs]; while ((line = reader.ReadLine()) != null) { if (line == "") { continue; } if (CountEdges == 0) { param = line.Split(' '); CountVertices = int.Parse(param[0]); CountEdges = int.Parse(param[1]); i++; g[i] = new DirectedGraph(CountVertices); continue; } param = line.Split(' '); int from = Int32.Parse(param[0]) - 1; int to = Int32.Parse(param[1]) - 1; g[i].AddEdge(from, to); CountEdges--; } } } catch (Exception e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } }
internal static DirectedGraph CreateReverseGraph(DirectedGraph g) { var countVerices = g.g.Length; DirectedGraph ng = new DirectedGraph(countVerices); for (int v = 0; v < countVerices; v++) { foreach (int n in g.g[v]) { ng.AddEdge(n, v); } } return ng; }
public MetaGraph(DirectedGraph g) { // do dfs on graph this.g = g; int l = this.g.g.Length; marked = new bool[l]; id = new int[l]; for (int v = 0; v < g.g.Length; v++) { if (marked[v]) { continue; } Dfs(v); } // do dfs on reverse (transposed) graph this.g = Algorithms.CreateReverseGraph(this.g); for (int i = 0; i < marked.Length; i++) { marked[i] = false; } //while(stack.Count != 0) foreach(int v in stack) { //int v = stack.Pop(); if (marked[v]) { continue; } DfsReverse(v); count++; } this.g = g; }
public Dfs(DirectedGraph g, int v) { marked = new bool[g.g.Length]; dfs(g, v); }
private void dfs(DirectedGraph g, int v) { if (marked[v]) { return; } marked[v] = true; foreach (int w in g.g[v]) { dfs(g, w); } }
public FloydWarshal(DirectedGraph g) { int countVertices = g.g.Length; dist = new int[countVertices, countVertices]; for (int i = 0; i < countVertices; i++) { for (int j = 0; j < countVertices; j++) { dist[i, j] = Int32.MaxValue / 4; } } for (int i = 0; i < countVertices; i++) { dist[i, i] = 0; } for (int i = 0; i < countVertices; i++) { foreach (int v in g.g[i]) { dist[i, v] = 1; } } for (int k = 0; k < countVertices; k++) { for (int i = 0; i < countVertices; i++) { for (int j = 0; j < countVertices; j++) { dist[i, j] = Math.Min(dist[i, j], dist[i, k] + dist[k, j]); //if (dist[i,j] > dist[i,k] + dist[k,j]) //{ // dist[i,j] = dist[i,k] + dist[k,j]; //} } } } }
public static int SemiConnected1(DirectedGraph g) { // http://himangi774.blogspot.ru/2013/12/check-if-graph-is-strongl-connected.html Algorithms.SCC scc = new Algorithms.SCC(g); //foreach (var v in scc.stack) //{ // Console.Write(v + " "); //} //Console.WriteLine(); //while (scc.stack.Count != 0) //{ // Console.Write(scc.stack.Pop() + " "); //} //Console.WriteLine(); var prev = scc.stack.Pop(); while (scc.stack.Count != 0) { var curr = scc.stack.Pop(); if (curr != prev - 1) { return -1; } prev = curr; } return 1; }
public static int SemiConnected0(DirectedGraph g) { Algorithms.FloydWarshal fw = new Algorithms.FloydWarshal(g); int il = fw.dist.GetLength(0); int jl = fw.dist.GetLength(1); //for (int i = 0; i < il; i++) //{ // for (int j = 0; j < jl; j++) // { // Console.Write(fw.dist[i, j] + " "); // } // Console.WriteLine(); //} //Console.WriteLine(); bool res; for (int i = 0; i < il; i++) { res = true; for (int j = 0; j < jl; j++) { if (fw.dist[i,j] == Int32.MaxValue / 4) { res = false; break; } } if (res) { return 1; } } return -1; }
public static int ReachableFrom(DirectedGraph g) { for (int i = 0; i < g.g.Length; i++) { Algorithms.Dfs dfs = new Algorithms.Dfs(g, i); //if (dfs.GetResult()) //{ // return i + 1; //} bool res = true; for (int j = 0; j < dfs.marked.Length; j++) { if (dfs.marked[j] == false) { res = false; break; } } if (res) { return i + 1; } } return -1; }