/// <summary>Serializes a graph into a file</summary> /// <param name="g">Graph to serialize</param> /// <param name="file">Target file</param> public static void Serialize(Graph g, string file) { using (XmlTextWriter writer = new XmlTextWriter(file, Encoding.UTF8)) { writer.Formatting = Formatting.Indented; writer.Indentation = 1; writer.IndentChar = '\t'; writer.WriteStartDocument(true); writer.WriteStartElement("GraphXML"); writer.WriteStartElement("graph"); writer.WriteAttributeString("id", "autogenerated_webgraph_10"); writer.WriteAttributeString("meta", g.Tag.ToString()); g.ForAllNodes(node => { writer.WriteStartElement("node"); writer.WriteAttributeString("name", string.Format("n{0:D4}", node.Id)); writer.WriteElementString("label", node.Label); writer.WriteEndElement(); // node }); g.ForAllEdges(edge => { writer.WriteStartElement("edge"); writer.WriteAttributeString("source", string.Format("n{0:D4}", edge.From.Id)); writer.WriteAttributeString("target", string.Format("n{0:D4}", edge.To.Id)); writer.WriteEndElement(); // edge }); writer.WriteEndElement(); // graph writer.WriteEndElement(); // GraphXML writer.Close(); } }
/// <summary> /// /// </summary> /// <param name="file"></param> /// <returns></returns> public static Graph Deserialize(string file) { XmlDocument doc = new XmlDocument(); doc.Load(file); Graph result = new Graph(); result.Tag = doc.SelectSingleNode("/GraphXML/graph").Attributes["meta"].Value; foreach (XmlNode node in doc.SelectNodes("/GraphXML/graph/node")) { Node n = result.AddNode(node.SelectSingleNode("label").InnerText); n.Id = Convert.ToInt32(node.Attributes["name"].Value.Substring(1)); } foreach (XmlNode edge in doc.SelectNodes("/GraphXML/graph/edge")) { int id1 = Convert.ToInt32(edge.Attributes["source"].Value.Substring(1)); int id2 = Convert.ToInt32(edge.Attributes["target"].Value.Substring(1)); result.AddEdge(result.FindNode(id1), result.FindNode(id2)); } return result; }
/// <summary>Initialization ctor </summary> /// <param name="g">Graph to layout</param> /// <param name="dRigidity">Initial layout speed</param> public GraphLayouter(Graph g, double dRigidity = 0.25) { graph = g; damping = true; rigidity = dRigidity; }