public void AddEdge(compV vertex) { if (!lst_neighbor.Contains(vertex)) { lst_neighbor.Add(vertex); vertex.lst_neighbor.Add(this); } }
public static void AddEdge_byID(ushort ID1, ushort ID2) { if (ID1 != ID2) { compV v1 = get_vertex(ID1); compV v2 = get_vertex(ID2); v1.AddEdge(v2); } }
public static List <compV> get_Componenta(compV v) //найти компоненту связности для произвольной вершины { List <compV> lst = new List <compV>(); //те вершины которые будут искать соседей List <compV> used = new List <compV>(); //использованные вершины lst.Add(v); while (lst.Count != 0) { compV t = lst[0]; used.Add(t); lst.Remove(t); foreach (var a in t.lst_neighbor) { if (!used.Contains(a) && !lst.Contains(a)) { lst.Add(a); } } } return(used); }
static void Main(string[] args) { compV.read_graph("f.txt"); for (ushort i = 0; i < 100; i++) { compV t = new compV(i); } Random q = new Random(); ushort id1, id2; //TODO:Почитать закономерность Ребра-вершины for (int i = 0; i < 500; i++) { id1 = (ushort)q.Next(0, 100); id2 = (ushort)q.Next(0, 100); compV.AddEdge_byID(id1, id2); } compV start = null; List <List <compV> > cmp_lst = new List <List <compV> >(); //список компонент связности на перспективу(?) while (true) //Пои тогам получим связный граф с одной компонентой связности { start = compV.all_vertex[0]; List <compV> componenta = start.get_Componenta(); if (componenta.Count != compV.all_vertex.Count) { //случай когда есть несколько компонент связности if (componenta.Count > compV.all_vertex.Count / 2) { for (int i = 0; i < compV.all_vertex.Count; i++) { if (!componenta.Contains(compV.all_vertex[i])) { compV.all_vertex[i].remove_vertex(); i--; } } } else { compV.remove_Componenta(componenta); } } else { break;//все вершины связанны между собой; } } Console.WriteLine(compV.all_vertex.Count); start = compV.all_vertex[0]; List <compV> botnet = new List <compV>(); //List<compV> new_bot = new List<compV>(); botnet.Add(start); //new_bot.Add(start); start.status = true; int step = 1; int count = 1; Console.WriteLine("Start vertex = " + start.ID); while (botnet.Count != compV.all_vertex.Count) { Console.WriteLine("Step " + step + " botnet count = " + botnet.Count + " new= " + count); count = 0; int c = botnet.Count; for (int i = 0; i < c; i++) { List <compV> atLst = botnet[i].attack(); foreach (var j in atLst) { Console.Write(botnet[i].ID + " -> " + j.ID + " "); j.status = true; botnet.Add(j); count++; } } step++; Console.WriteLine(); } /* Среднее количество ребер * float sr = 0; * foreach (var a in compV.all_vertex) * { * sr += a.countEdge; * } * sr /= 100; * Console.WriteLine(sr); */ Console.ReadKey(); }