/// <summary> /// Чете граф от файл. Формат - списък на съседи /// </summary> /// <param name="filename"></param> /// <param name="graph"></param> public static void ReadGraphFile(string filename, ref Graph graph) { String[] lines = File.ReadAllLines(filename); int lineIdx = 0; try { String[] parts = lines[0].Split(' '); int n = int.Parse(parts[0]); int m = int.Parse(parts[1]); Vertex.ResetCounter(); for (int i = 0; i < n; i++) graph.vertices.Add(new Vertex()); lineIdx++; for(int i = 0; i < m; i++) { parts = lines[lineIdx].Split(' '); int src = int.Parse(parts[0]); int dest = int.Parse(parts[1]); Edge e = graph.AddEdge(src, dest); if(parts.Length > 2) { // част за четене на тегла int val = 0; try { val = int.Parse(parts[2]); int propId = Property.GetPropertyId("тегло"); Property.SetSpecialProperty(propId, SpecialProperty.EdgeWeight); e.SetProperty(propId, int.Parse(parts[2])); } catch(Exception exc) { Console.WriteLine(String.Format("Line {0}: third part was NaN: {1}", lineIdx, parts[2])); } } lineIdx++; } } catch(Exception e) { MessageBox.Show(String.Format("An exception occured on line {0}: {1}", lineIdx, e.Message), e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error); graph = new Graph(); Vertex.ResetCounter(); return; } graph.ArrangeInCircle(); }
void menu_Generate(UiComponent sender, Object arg) { Vertex.ResetCounter(); Graph g = new Graph(); int n = random.Next(50); int m = random.Next(100); for (int i = 0; i < n; i++) { Vertex v = new Vertex((float)random.NextDouble() * 1000 - 500.0f, (float)random.NextDouble() * 600 - 300.0f); g.AddVertex(v); if (i == 0) continue; int connections = random.Next(1, (int)Math.Min(i, 3)); for(int j = 0; j < connections; j++) { int vo = random.Next(0, i); bool contains = false; foreach(Edge e in v.edges) { if(e.source.id == vo || e.destination.id == vo) { contains = true; break; } } if (contains) j--; else g.AddEdge(v.id, vo); } } fs.SetForce(50); activeGraph = g; fs.SetGraph(g); }
/// <summary> /// Създава първоначален граф /// </summary> void MakeGraph() { // ИЗВЪН ЕКСПЛОАТАЦИЯ //Property.GetPropertyId("цвят") Property.GetPropertyId("име"); Property.GetPropertyId("цена"); Property.GetPropertyId("поток"); Property.GetPropertyId("обратен поток"); activeGraph = new Graph(); Vertex v = new Vertex(0, 0); v.SetProperty(0, "Варна"); Vertex v1 = new Vertex(100, 0); v1.SetProperty(0, "София"); v1.SetProperty(1, 512.25f); Vertex v2 = new Vertex(100, 0); v2.SetProperty(0, "Шумен"); v2.SetProperty(2, 112.125f); Vertex v3 = new Vertex(100, 0); v3.SetProperty(0, "Стара Загора"); v3.SetProperty(3, 12.725f); activeGraph.AddVertex(v); activeGraph.AddVertex(v1); activeGraph.AddVertex(v2); activeGraph.AddVertex(v3); Edge e1 = activeGraph.AddEdge(0, 3); e1.SetProperty(2, 200); }
/// <summary> /// Чете данните за графа и го създава /// </summary> /// <param name="graphDataPtr">указател към буфера</param> void ReadGraphData(IntPtr graphDataPtr) { GraphicScheme.LoadFont(); byte[] gdBuff = new byte[9]; // информация за графа: [брой върхове] [брой ребра] [насочен] int nRead; bool result = NativeMethods.ReadProcessMemory(processHandle, graphDataPtr, gdBuff, 9, out nRead); int[] count = new int[2]; Buffer.BlockCopy(gdBuff, 0, count, 0, 8); //MessageBox.Show(String.Format("Vertices: {0}, Edges: {1}", count[0], count[1])); graph = new Graph(gdBuff[8]!=0); graph.vertices = new List<Vertex>(count[0]); Vertex.ResetCounter(); // better safe than sorry Edge.ResetCounter(); for(int i = 0; i < count[0]; i++) { graph.AddVertex(new Vertex()); } int[] edges = new int[count[1] * 2]; byte[] edgeBuff = new byte[count[1] * 8]; result = NativeMethods.ReadProcessMemory(processHandle, graphDataPtr + 9, edgeBuff, count[1] * 8, out nRead); Buffer.BlockCopy(edgeBuff, 0, edges, 0, count[1] * 8); for(int i = 0; i < count[1]; i++) { graph.AddEdge(edges[2 * i], edges[2 * i + 1]); } }