private void SetCycleCostAroundAnc(PlanarNode anc, PlanarNode last, ref int sum0, ref int sum1) { // return; PlanarEdge pn = null; PlanarEdge nch = null; foreach (int eid in anc.edgesIds) { if (g.planarEdges[eid].GetNeigh(anc).state == -3) //count=2 { if (g.planarEdges[eid].GetNeigh(anc).nid == last.nid) { nch = g.planarEdges[eid]; if (pn == null) { pn = nch; //src muze mit jen jednu hr. } } else { pn = g.planarEdges[eid]; } } } ; SetCycleCostAroundN(anc, pn, nch, ref sum0, ref sum1); }
private static void InsertAsFirst(PlanarNode n, int eid, int newEid) { if (n.edgesIds.Contains(eid)) { n.edgesIds.Insert(n.edgesIds.IndexOf(eid), newEid); } }
public List <PlanarNode> SeparateByEdge( PlanarEdge f, PlanarNode src, out List <PlanarNode> inC, out List <PlanarNode> outC) { Bfs.Src_all_bfs(src, g.planarNodes, g.planarEdges, true); SetCycleCost(f); Bfs.Src_all_bfs(src, g.planarNodes, g.planarEdges, true); inC = new List <PlanarNode>(); outC = new List <PlanarNode>(); List <PlanarNode> sep = new List <PlanarNode>(); foreach (PlanarNode n in g.planarNodes.Values) { if (cycle.Contains(n.nid)) { continue; } if (n.insideCycle) { inC.Add(n); n.insideCycle = false; } else { outC.Add(n); } } return(sep); }
private static bool FindSeparatorOnTriangulation(PlanarNode src, PlanarGraph pg, SeparatorCycle sepCycle) { pg = Triangulation.GetTriangulation(pg); foreach (PlanarEdge f in Triangulation.triEdges) { if (((PlanarNode)f.neighboursAdjEdges[0]).nid == srcId || ((PlanarNode)f.neighboursAdjEdges[1]).nid == srcId) { continue; } PlanarNode src2 = pg.planarNodes[srcId]; Bfs.Src_all_bfs(src2, pg.planarNodes, pg.planarEdges, true); sepCycle.GetBoundaryCycle(pg, src2, f); int max = (int)(c * pg.planarNodes.Count); if (sepCycle.inC.Count < max && sepCycle.outC.Count < max) { Console.WriteLine(gc + " " + sepCycle.inC.Count); // ResetGraph(pg); return(true); } ResetGraph(pg); // break; } Console.WriteLine("not: " + gc + " " + pg.planarNodes.Count); return(false); }
private void SetCycleCostUpward(ref PlanarNode n, PlanarEdge pn, PlanarEdge nch, out PlanarNode last, ref int sum0, ref int sum1) { PlanarNode par = n.parent; last = n; // while (par!=null && par.nid!=commonAnc.nid) while (n != null && n.nid != commonAnc.nid) { SetCycleCostAroundN(n, pn, nch, ref sum0, ref sum1); nch = pn; last = n; n = n.parent; if (n.parent == null) //||(reversed&&n.parent.state==-3)) { break; } foreach (int eid in n.edgesIds) { if (g.planarEdges.ContainsKey(eid) && g.planarEdges[eid].GetNeigh(n).nid == n.parent.nid) { pn = g.planarEdges[eid]; break; } } } }
GetSeparationTree(PlanarGraph pg, PlanarNode src) //1610738 78794 { pg = Triangulation.GetTriangulation(pg); PlanarEdge ww = pg.planarEdges[1630674]; Dictionary <PlanarEdge, int> cyclesTriEdges = new Dictionary <PlanarEdge, int>(); StreamReader r = new StreamReader("C:\\Users\\L\\Desktop\\triCycles.txt"); while (!r.EndOfStream) { string line = r.ReadLine(); string[] spl = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); cyclesTriEdges.Add( pg.planarEdges[int.Parse(spl[0])], int.Parse(spl[0])); } int max = cyclesTriEdges.Values.Max(); KeyValuePair <PlanarEdge, int> maxTriE = cyclesTriEdges.Where(x => x.Value == max).First(); cyclesTriEdges.Remove(maxTriE.Key); SeparatorCycle cycle = new SeparatorCycle(pg); List <PlanarNode> inC; List <PlanarNode> outC; List <PlanarNode> sep = cycle.SeparateByEdge( pg.planarEdges[1630674], pg.planarNodes.First().Value, out inC, out outC); return(null); // DecompositionTreeItem rootIte }
/////////////////////////////// public void GetPlanarGraphBNF(Graph graph, List <PlanarNode> plNodes, Dictionary <int, PlanarEdge> plE) { Dictionary <long, Node> nodes = graph.nodes; Node s = nodes[plNodes.First().nid]; file = new StreamWriter(name); WriteHeader(s, s, 50, plNodes.Count); List <Node> one = new List <Node>(); foreach (Node n in nodes.Values) { // n.SetData2(graph.maxLat, graph.minLon); n.Resize(10); // if (!n.inside) // AddPoint(n.coordinates.pictureCrd2, n.coordinates.pictureCrd1, "black", 0.3); } foreach (PlanarEdge e in plE.Values) { PlanarNode p0 = (PlanarNode)e.neighboursAdjEdges[0]; PlanarNode p1 = (PlanarNode)e.neighboursAdjEdges[1]; Node n0 = nodes[p0.nid]; Node n1 = nodes[p1.nid]; String color = "black"; // if (pe.parent != null) AddLine(n0.coordinates.pictureCrd1, n0.coordinates.pictureCrd2, n1.coordinates.pictureCrd1, n1.coordinates.pictureCrd2, color, 2); } AddPoint(s.coordinates.pictureCrd2, s.coordinates.pictureCrd1, "red", 3.0); WriteLastTagAndClose(); }
private static void RecursiveSeparation(PlanarGraph pg, PlanarNode src, List <PlanarNode> orderNodes, SeparatorCycle sepCycle) { PlanarEdge sepE; Bfs.Src_all_bfs(src, pg.planarNodes, pg.planarEdges, true); if (pg.planarNodes.Count < nodesCount / 10 || !FindSeparator(src, orderNodes, pg, sepCycle, out sepE)) { Console.WriteLine("Leaf:" + gc + " " + pg.planarNodes.Count); return; } List <PlanarNode> cycleNodes = sepCycle.cycle .Where(x => pg.planarNodes.Keys.Contains(x)) .Select(x => pg.planarNodes[x]).ToList(); gc++; PlanarGraph g0 = new PlanarGraph(sepCycle.inC, cycleNodes, src, new Dictionary <int, PlanarEdge>(pg.planarEdges)); PlanarGraph g1 = new PlanarGraph(sepCycle.outC, cycleNodes, src, new Dictionary <int, PlanarEdge>(pg.planarEdges)); RecursiveSeparation(g0, g0.planarNodes[srcId], orderNodes, sepCycle); gc++; Bfs.Src_all_bfs(src, pg.planarNodes, pg.planarEdges, true); RecursiveSeparation(g1, g1.planarNodes[srcId], orderNodes, sepCycle); }
public static void Src_all_bfs(PlanarNode src, Dictionary <long, PlanarNode> planarN, Dictionary <int, PlanarEdge> planarEdges, bool parentUpdate) { foreach (PlanarNode n in planarN.Values) { if (parentUpdate) { n.parent = null; n.children = new List <PlanarNode>(); n.dist = int.MaxValue; n.state = 0; } } PriorityQueue <PlanarNode> opened = new PriorityQueue <PlanarNode>(); // List<PlanarNode> opened = new List<PlanarNode>(); opened.Add(0, src); src.dist = 0; if (!parentUpdate) { part0 = new List <long>(); part0.Add(src.nid); } while (opened.Count > 0) { Close(opened, planarEdges, parentUpdate); } }
private bool Has_left_yz_path(PlanarNode z, PlanarNode zchild) { int lastEid = -1; foreach (int eid in z.edgesIds) { if (g.planarEdges[eid].GetNeigh(z).nid == zchild.nid) { lastEid = eid; break; } } PlanarEdge e = g.planarEdges[g.planarEdges[lastEid].GetNextEdgeId(z)]; PlanarNode u = e.GetNeigh(z); while (u.state > -2) { e = g.planarEdges[e.GetNextEdgeId(z)]; u = e.GetNeigh(z); } if (u.parent.nid == z.nid) { return(false); } return(true); }
public PlanarEdge(PlanarEdge e, PlanarNode oldNeigh, PlanarNode newNeigh) { new PlanarEdge(e); UpdateNeighbour(oldNeigh, newNeigh); newNeigh.edgesIds.Add(eid); w = oldNeigh.dist + e.w; }
public void CreatePlanarNodes() { foreach (KeyValuePair <long, List <int> > pair in nIdSortedEdgesId) { PlanarNode pn = AddAndGetNode(pair.Key); pn.edgesIds = pair.Value; } }
public PlanarEdgeRepre(PlanarNode n0, PlanarNode n1, PlanarEdge n0e) { arr[0] = n0; arr[1] = n1; arr[2] = n0e; //arr[4] = -1; //arr[5] = -1; }
public PlanarEdgeRepre(PlanarNode n0, PlanarNode n1, int n0EAid, int n0EBid)// PlanarEdge n0EA, PlanarEdge n0EB) { arr[0] = n0; arr[1] = n1; n0_EAid = n0EAid; n0_EBid = n0EBid; arr[4] = -1; arr[5] = -1; }
private static bool FindSeparator(PlanarNode src, List <PlanarNode> orderNodes, PlanarGraph pg, SeparatorCycle sepCycle, out PlanarEdge f) { List <PlanarNode> insideBoundaryNodes = orderNodes .Where(y => pg.planarNodes.ContainsKey(y.nid)).ToList(); for (int i = 0; i < insideBoundaryNodes.Count - 1; i = i + 1) { for (int j = i + 1; j < insideBoundaryNodes.Count; j = j + 1) { PlanarNode src2 = pg.planarNodes[srcId]; Bfs.Src_all_bfs(src2, pg.planarNodes, pg.planarEdges, true); PlanarNode u = pg.planarNodes[insideBoundaryNodes[i].nid]; PlanarNode v = pg.planarNodes[insideBoundaryNodes[j].nid]; f = new PlanarEdge(u, v); f.eid = -pg.planarEdges.Count - 1; f.trgl = true; pg.planarEdges.Add(f.eid, f); u.edgesIds.Add(f.eid); v.edgesIds.Add(f.eid); // SeparatorCycle sepCycle = new SeparatorCycle(); sepCycle.GetBoundaryCycle(pg, src2, f); int max = (int)(c * pg.planarNodes.Count); if (sepCycle.inC.Count < max && sepCycle.outC.Count < max) { Console.WriteLine(gc + " " + sepCycle.inC.Count + " " + u.nid + " " + v.nid); // ResetGraph(pg); return(true); } else { u.edgesIds.Remove(f.eid); v.edgesIds.Remove(f.eid); } ResetGraph(pg); // break; } } f = null; if (c == 4f / 5) { c = 19f / 20; return(FindSeparator(src, orderNodes, pg, sepCycle, out f)); } else { // c = 4f / 5; Console.WriteLine("not: " + gc + " " + pg.planarNodes.Count); return(FindSeparatorOnTriangulation(src, pg, sepCycle)); } }
private static void GetTriangulationFromTo(Dictionary <long, List <long> > tri, PlanarGraph g, PlanarNode n1, int eid0, int eid1) { try { if (!(g.planarEdges.ContainsKey(eid0) && g.planarEdges.ContainsKey(eid1))) { return; } PlanarEdge e0 = g.planarEdges[eid0]; PlanarEdge e1 = g.planarEdges[eid1]; PlanarNode n0 = e0.GetNeigh(n1); PlanarNode n2 = e1.GetNeigh(n1); if (!n2.edgesIds.Contains(e1.eid)) { return; } int fid = e1.GetNextEdgeId(n2); if (!(g.planarEdges.ContainsKey(fid))) { return; } PlanarEdge f = g.planarEdges[fid]; PlanarNode n3 = f.GetNeigh(n2); if (n0.nid == n3.nid) { return; //triangle } if (tri.ContainsKey(n0.nid) && tri[n0.nid].Contains(n2.nid)) { return; } if (!tri.ContainsKey(n0.nid) || (!tri.ContainsKey(n1.nid)) || (!tri.ContainsKey(n2.nid))) { return; } tri[n0.nid].Add(n2.nid); tri[n2.nid].Add(n0.nid); PlanarEdge te = new PlanarEdge(n0, n2, 0, 0, 0); te.trgl = true; te.eid = newId; newId++; InsertAsFirst(n0, eid0, te.eid); InsertAsSecond(n2, eid1, te.eid); g.planarEdges.Add(te.eid, te); triEdges.Add(te); //tri.Add(te.eid); } catch (Exception ex) { return; } }
private int GetInsideDescCostFromXToZAroundY(PlanarNode y, PlanarNode z, PlanarEdge xy, out PlanarEdge yz, bool leftpath) { if (leftpath) { return(GetDescCostFromZToXAroundY(y, z, xy, out yz)); } return(GetDescCostFromXToZAroundY(y, z, xy, out yz)); }
public static void UpdateNextEdges(PlanarNode src, Dictionary <int, PlanarEdge> planarEdges) //jen jeden smer { List <int> next = new List <int>(src.edgesIds); next.RemoveAt(0); next.Add(src.edgesIds.First()); foreach (int eid in src.edgesIds) { UpdateNextEdges(src, planarEdges[eid], planarEdges[next[0]]); } }
private int GetDescCost(PlanarNode v, PlanarNode w) { if (v.parent != null && v.parent.nid == w.nid) { return(allCostSum - v.cost); } if (w.parent != null && w.parent.nid == v.nid) { return(w.cost); } return(0); //not tree }
private PlanarEdge ExpandCycleWithTriangle(PlanarNode y, PlanarEdge yx, PlanarEdge treeEdge) { y.state = -3; PlanarEdge next = g.planarEdges[yx.GetNextEdgeId(y)]; // int yDescCost = GetInsideDescCostForNextTwoEdges(y,next, yx); int yDescCost = GetDescCost(y, next.GetNeigh(y)); insideDescCost = insideDescCost - yDescCost; outsideDescCost = outsideDescCost + yDescCost; return(next); }
public PlanarNode GetNeigh(PlanarNode n) { if (n.nid == ((PlanarNode)neighboursAdjEdges[0]).nid) { return((PlanarNode)neighboursAdjEdges[1]); } else if (n.nid == ((PlanarNode)neighboursAdjEdges[1]).nid) { return((PlanarNode)neighboursAdjEdges[0]); } return(null); }
public PlanarEdge GetNextEdge(PlanarNode n) { if (n == neighboursAdjEdges[0]) { return((PlanarEdge)neighboursAdjEdges[2]); } if (n == neighboursAdjEdges[1]) { return((PlanarEdge)neighboursAdjEdges[4]); } return(null); }
//step 6 private PlanarGraph CreateShrinkedGraph(Dictionary <int, List <long> > levels, PlanarGraph pg, long srcNid) { Dictionary <long, PlanarNode> originalNodes = new Dictionary <long, PlanarNode>(pg.planarNodes); Dictionary <long, PlanarNode> shrNodes = new Dictionary <long, PlanarNode>(); foreach (int key in levels.Keys) { if (key >= lev0)// && key <= lev2) { foreach (long nid in levels[key]) { PlanarNode n = pg.planarNodes[nid]; shrNodes.Add(n.nid, n); } } } PlanarNode src2 = originalNodes[srcNid]; shrNodes.Add(src2.nid, src2); foreach (PlanarNode n in shrNodes.Values) { break; List <int> newEdgesIds = new List <int>(); foreach (int eid in n.edgesIds) { PlanarEdge e = pg.planarEdges[eid]; PlanarNode neigh = e.GetNeigh(n); if (shrNodes.ContainsKey(neigh.nid)) { newEdgesIds.Add(eid); } } n.edgesIds = newEdgesIds; } Dictionary <int, PlanarEdge> shrEdges = new Dictionary <int, PlanarEdge>(); foreach (PlanarEdge e in pg.planarEdges.Values) { break; if (shrNodes.ContainsKey( ((PlanarNode)e.neighboursAdjEdges[0]).nid) && shrNodes.ContainsKey( ((PlanarNode)e.neighboursAdjEdges[1]).nid)) { shrEdges.Add(e.eid, e); } } GoAroundBnfTree(pg.planarNodes, levels, src2); return(new PlanarGraph(shrNodes, shrEdges)); }
private static void CheckEdgesIds(PlanarGraph g, PlanarNode n) { List <int> tmp = new List <int>(); foreach (int eid in n.edgesIds) { if (g.planarEdges.ContainsKey(eid)) { tmp.Add(eid); } } n.edgesIds = tmp; }
public int GetNextEdgeId(PlanarNode n) { int index = n.edgesIds.IndexOf(eid); if (index == n.edgesIds.Count - 1) { return(n.edgesIds[0]); } else { return(n.edgesIds[index + 1]); } }
public PlanarNode GetNextEdgeId(PlanarNode n) { if (n == arr[0]) { return((PlanarNode)arr[2]); } if (n == arr[1]) { return((PlanarNode)arr[4]); } return(null); }
private static void InsertAsSecond(PlanarNode n, int eid, int newEid) { int i0 = n.edgesIds.IndexOf(eid); if (i0 == n.edgesIds.Count - 1) { n.edgesIds.Add(newEid); } else { n.edgesIds.Insert(i0 + 1, newEid); } }
bool Check(PlanarEdge e, PlanarNode n, PlanarNode src2, Dictionary <long, bool> table) { PlanarNode neigh = e.GetNeigh(n); if (table[neigh.nid]) { return(false); } table[neigh.nid] = true; e.UpdateNeighbour(n, src2); // src2.edgesIds.Add(e.eid); return(true); }
private void InitNewCycleLeaves(PlanarNode u, PlanarNode y, out PlanarNode right, out PlanarNode left, bool left_yz_path) { if (left_yz_path) { left = y; right = u; } else { left = u; right = y; } }
public static void GetDecompositionTree(PlanarGraph pg, Graph g) { nodesCount = g.nodes.Count(); List <PlanarNode> orderNodes = OrderBoundaryNodes(pg, g, srcId); PlanarNode src = pg.planarNodes[srcId]; // .First().Value; srcId = src.nid; SeparatorCycle sepCycle = new SeparatorCycle(); RecursiveSeparation(pg, src, orderNodes, sepCycle); Console.ReadKey(); }