public void TestGetCrossingEdgesForMinCut() { UGraph graph = GetGraphObject(); LinkedList <Edge> crossingEdgesForMinCut = graph.GetCrossingEdgesForMinCut(); bool result = false; if (crossingEdgesForMinCut.Count == 2) { result = true; } //else //{ // int e1HeadLbl = crossingEdgesForMinCut.First.Value.Head.Label; // int e1TailLbl = crossingEdgesForMinCut.First.Value.Tail.Label; // int e2HeadLbl = crossingEdgesForMinCut.Last.Value.Head.Label; // int e2TailLbl = crossingEdgesForMinCut.Last.Value.Tail.Label; // if (((e1HeadLbl == 4 && e1TailLbl == 5 || (e1HeadLbl == 5 && e1TailLbl == 4)) // && (e2HeadLbl == 3 && e2TailLbl == 6 || (e2HeadLbl == 6 && e2TailLbl == 3))) // || ((e1HeadLbl == 3 && e1TailLbl == 6 || (e1HeadLbl == 6 && e1TailLbl == 3)) // && (e2HeadLbl == 4 && e2TailLbl == 5 || (e2HeadLbl == 5 && e2TailLbl == 4)))) // result = true; //} Assert.AreEqual(true, result, crossingEdgesForMinCut.Count.ToString()); }
public void Find_connected_componenets() { UGraph<int> g; using (var sr = File.OpenText(@"C:\rajiv\DSinCS\DataStructures\DSTests\Data\tinyG.txt")) { var v = UInt32.Parse(sr.ReadLine()); sr.ReadLine(); g = new UGraph<int>(v); while (!sr.EndOfStream) { var line = sr.ReadLine(); if(line == null) { continue; } var nums = line.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse) .ToArray(); g.AddEdge(nums[0], nums[1]); } } var res = g.FindCC(); Assert.AreEqual(res.Count, 3); Assert.AreEqual("0,1,2,3,4,5,6", string.Join(",", res[0].Sort())); Assert.AreEqual("7,8", string.Join(",", res[2].Sort())); Assert.AreEqual("9,10,11,12", string.Join(",", res[1].Sort())); }
public void TestGraphConstructor() { UGraph graph = GetGraphObject(); List <Edge> correctEdgeList = new List <Edge>() { new Edge(graph.Vertices[1 - 1], graph.Vertices[2 - 1]), new Edge(graph.Vertices[1 - 1], graph.Vertices[3 - 1]), new Edge(graph.Vertices[1 - 1], graph.Vertices[4 - 1]), new Edge(graph.Vertices[2 - 1], graph.Vertices[3 - 1]), new Edge(graph.Vertices[2 - 1], graph.Vertices[4 - 1]), new Edge(graph.Vertices[4 - 1], graph.Vertices[3 - 1]), new Edge(graph.Vertices[4 - 1], graph.Vertices[5 - 1]), new Edge(graph.Vertices[3 - 1], graph.Vertices[6 - 1]), new Edge(graph.Vertices[5 - 1], graph.Vertices[6 - 1]), new Edge(graph.Vertices[5 - 1], graph.Vertices[8 - 1]), new Edge(graph.Vertices[6 - 1], graph.Vertices[7 - 1]), new Edge(graph.Vertices[5 - 1], graph.Vertices[7 - 1]), new Edge(graph.Vertices[6 - 1], graph.Vertices[8 - 1]), new Edge(graph.Vertices[7 - 1], graph.Vertices[8 - 1]), }; bool areEdgesCreatedProperly = true; if (graph.Edges.Count == 14) { foreach (Edge e in correctEdgeList) { List <Edge> matchingEdgesInGraph = graph.Edges.FindAll(x => (x.Head == e.Head && x.Tail == e.Tail) || (x.Tail == e.Head && x.Head == e.Tail)); if (matchingEdgesInGraph.Count != 1) { areEdgesCreatedProperly = false; break; } } } else { areEdgesCreatedProperly = false; } Assert.AreEqual(true, areEdgesCreatedProperly, graph.Edges.Count.ToString()); }
public void TestAreSameSortedMergedVertex() { UGraph graph = GetGraphObject(); ContractibleGraph cGraph = new ContractibleGraph(graph); LinkedList <Vertex> vertices1 = new LinkedList <Vertex>(); vertices1.AddLast(new LinkedListNode <Vertex>(graph.Vertices[0])); vertices1.AddLast(new LinkedListNode <Vertex>(graph.Vertices[1])); vertices1.AddLast(new LinkedListNode <Vertex>(graph.Vertices[2])); LinkedList <Vertex> vertices2 = new LinkedList <Vertex>(); vertices2.AddLast(new LinkedListNode <Vertex>(graph.Vertices[0])); vertices2.AddLast(new LinkedListNode <Vertex>(graph.Vertices[1])); Assert.AreEqual(false, cGraph.AreSameSortedMergedVertex(vertices1, vertices2)); }
public void TestContractEdge() { UGraph graph = GetGraphObject(); ContractibleGraph cGraph = new ContractibleGraph(graph); ContractibleEdge cEdge = new ContractibleEdge(); cEdge.Head = new LinkedList <Vertex>(); cEdge.Head.AddLast(graph.Vertices[0]); cEdge.Head.AddLast(graph.Vertices[2]); cEdge.Head.AddLast(graph.Vertices[4]); cEdge.Tail = new LinkedList <Vertex>(); cEdge.Tail.AddLast(graph.Vertices[1]); cEdge.Tail.AddLast(graph.Vertices[3]); cEdge.Tail.AddLast(graph.Vertices[5]); bool result = true; LinkedList <Vertex> sortedMergedVertex = cGraph.GetSortedMergedVertex(cEdge); if (sortedMergedVertex.Count == 6) { int prevVertexLabel = sortedMergedVertex.First.Value.Label; foreach (Vertex v in sortedMergedVertex) { if (v.Label < prevVertexLabel) { result = false; break; } prevVertexLabel = v.Label; } } else { result = false; } Assert.AreEqual(true, result); }
public void TestContractEdgeAndDeleteSelfLoops() { UGraph graph = GetGraphObject(); ContractibleGraph cGraph = new ContractibleGraph(graph); Edge originalEdgeContracted = cGraph.ContractibleEdges.First.Value.OriginalEdge; //Test after 1 contraction cGraph.ContractEdgeAndDeleteSelfLoops(cGraph.ContractibleEdges.First.Value); bool result = true; if (cGraph.ContractibleEdges.Count != 13) { result = false; } else { int count = 0; foreach (ContractibleEdge ce in cGraph.ContractibleEdges) { //fails if edge being contracted is still present if (ce.OriginalEdge == originalEdgeContracted) { result = false; break; } //Check no self loops - Note that both head and tails are sorted if (ce.Head.Count == ce.Tail.Count) { LinkedListNode <Vertex> tailCurrNode = ce.Tail.First, headCurrNode = ce.Head.First; bool areEndsDiff = false; for (int i = 0; i < ce.Tail.Count; i++) { if (tailCurrNode.Value.Label != headCurrNode.Value.Label) { areEndsDiff = true; break; } } if (!areEndsDiff) { result = false; break; } } //Check for edges [(1,2),3],[(1,2),4], [1,2 //Check for (1,2),3 - 2 in no if ((ce.OriginalEdge.Head.Label == 1 && ce.OriginalEdge.Tail.Label == 3) || (ce.OriginalEdge.Head.Label == 3 && ce.OriginalEdge.Tail.Label == 1) || (ce.OriginalEdge.Head.Label == 3 && ce.OriginalEdge.Tail.Label == 2) || (ce.OriginalEdge.Head.Label == 2 && ce.OriginalEdge.Tail.Label == 3)) { if ((ce.Head.Count == 2 && ce.Head.Contains(cGraph.OriginalGraph.Vertices[0]) && ce.Head.Contains(cGraph.OriginalGraph.Vertices[1]) && ce.Tail.Count == 1 && ce.Tail.Contains(cGraph.OriginalGraph.Vertices[2])) || (ce.Tail.Count == 2 && ce.Tail.Contains(cGraph.OriginalGraph.Vertices[0]) && ce.Tail.Contains(cGraph.OriginalGraph.Vertices[1]) && ce.Head.Count == 1 && ce.Head.Contains(cGraph.OriginalGraph.Vertices[2]))) { count = count + 1; } } } if (result == false || (result == true && count != 2)) { result = false; } } Assert.AreEqual(true, result); }
private static void ClientPart() { while (true) { x2: Console.Write($"Welcome {ua.FullName}\n1 for add an application\n2 for show your history of applications\n3-for show your aceped apps with graphic\n4 for Exit\n"); int chs = 0; try { chs = int.Parse(Console.ReadLine()); } catch { Console.WriteLine("Write a number!"); goto x2; } if (chs == 1) { UApp uapp = new UApp(); c1: Console.Write("Credit sum:"); try { uapp.CreditSum = (double.Parse(Console.ReadLine())); } catch { Console.WriteLine("Write a number"); goto c1; } Console.Write("Credit goal:"); uapp.CreditGoal = Console.ReadLine(); Console.Write("How many month do you need?"); uapp.CreditDeadLine = double.Parse(Console.ReadLine()); c2: Console.Write("You payment:"); try { uapp.Pay = double.Parse(Console.ReadLine()); } catch { Console.WriteLine("Write a number"); goto c2; } bool st = CountP(uapp); uapp.Status = st; uapp.UId = ua.id; uapp.Add(uapp); if (uapp.Status) { double pmonth = uapp.CreditSum / uapp.CreditDeadLine; SqlConnection cn = new SqlConnection(@"Data Source = localhost;Initial Catalog = CADB; Integrated Security=True;"); cn.Open(); string cm = $"select * from U_App where UId = { ua.id } and CreditGoal = '{uapp.CreditGoal}' and CreditDeadLine = {uapp.CreditDeadLine}"; SqlCommand cd = new SqlCommand(cm, cn); SqlDataReader r = cd.ExecuteReader(); UApp ml = new UApp(); while (r.Read()) { ml = new UApp() { id = int.Parse(r.GetValue("id").ToString()), UId = int.Parse(r.GetValue("UId").ToString()), CreditSum = double.Parse(r.GetValue("CreditSum").ToString()), CreditGoal = r.GetValue("CreditGoal").ToString(), CreditDeadLine = double.Parse(r.GetValue("CreditDeadLine").ToString()), Status = bool.Parse(r.GetValue("Status").ToString()), Pay = double.Parse(r.GetValue("Pay").ToString()) }; } cn.Close(); UGraph ug = new UGraph() { PMonth = pmonth, Months = uapp.CreditDeadLine, U_AppId = ml.id }; ug.Add(ug); } string acceped = uapp.Status ? "aceped" : "refused"; Console.ForegroundColor = uapp.Status ? ConsoleColor.Green : ConsoleColor.Red; Console.WriteLine($"You credit status {acceped}"); Console.ForegroundColor = ConsoleColor.Black; } else if (chs == 2) { UApp uapp = new UApp(); var li = uapp.SingleAllById(ua.id); Console.WriteLine($"ID\tCreditSum\tCreditDeadline\tCreditStatus"); foreach (var x in li) { System.Console.WriteLine("===================================="); string acpd = x.Status ? "aceped" : "canceled"; Console.WriteLine($"{x.id}\t\t{x.CreditSum}\t\t{x.CreditDeadLine}\t\t{acpd}"); System.Console.WriteLine("===================================="); } } else if (chs == 3) { UApp uapp = new UApp(); var li = uapp.SingleAllById(ua.id); Console.WriteLine($"ID\tCreditSum\tCreditDeadline\tCreditStatus\tPayed"); foreach (var x in li) { System.Console.WriteLine("===================================="); Console.WriteLine($"{x.id}\t\t{x.CreditSum}\t\t{x.CreditDeadLine}\t{x.Status}\t\t{x.Done}"); System.Console.WriteLine("===================================="); UGraph ug = new UGraph(); ug = ug.SingleById(x.id); Console.WriteLine("\tId\tPer Month\tMonths"); Console.WriteLine($"\t{ug.id}\t{ug.PMonth}\t\t{ug.Months}"); System.Console.WriteLine("===================================="); } Console.WriteLine("Do you have money for paying this month?Y/N"); char ches = char.Parse(Console.ReadKey().KeyChar.ToString().ToLower()); Console.WriteLine(); if (ches == 'y') { d1: Console.WriteLine("Enter ID of app from sight graph"); int id = 0; try { id = int.Parse(Console.ReadLine()); } catch { Console.WriteLine("Please choose number"); goto d1; } SqlConnection cn = new SqlConnection(@"Data Source = localhost;Initial Catalog = CADB; Integrated Security=True;"); cn.Open(); string cm = $"select * from U_Graph where id={id}"; SqlCommand cd = new SqlCommand(cm, cn); SqlDataReader r = cd.ExecuteReader(); string cmd = ""; while (r.Read()) { cmd = $"update U_Graph set Months = {int.Parse(r.GetValue("Months").ToString()) - 1} where id = {id}"; } r.Close(); cd.CommandText = cmd; cd.ExecuteNonQuery(); cn.Close(); } } else { break; } } }