📄 graph.cs
字号:
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
namespace MyGraph
{
public class Vertex
{
public int vtx;
public LinkedList<EdgeLink> list;
public Vertex()
{
vtx = -1;
list = new LinkedList<EdgeLink>();
}
public Vertex(int v)
{
vtx = v;
list = new LinkedList<EdgeLink>();
}
public void DeleteEdge(int to)
{
LinkedListNode<EdgeLink> temp = list.First;
while ((temp.Value.destVtx != to) && (temp != null))
temp = temp.Next;
list.Remove(temp);
}
}
public class Graph
{
public LinkedList<Vertex> VertexList;
public Graph()
{
VertexList = new LinkedList<Vertex>();
}
public LinkedListNode<Vertex> FindVetex(int vtx)
{
LinkedListNode<Vertex> temp = VertexList.First;
while (temp != null)
{
if (temp.Value.vtx == vtx)
break;
temp = temp.Next;
}
return temp;
}
public void InsertVertex(int vtx)
{
if (FindVetex(vtx) == null)
{
Vertex tempvtx = new Vertex(vtx);
VertexList.AddLast(tempvtx);
}
}
public void InsertEdge(int from, int to)
{
if (FindVetex(from) == null)
InsertVertex(from);
if (FindVetex(to) == null)
InsertVertex(to);
LinkedListNode<Vertex> tempVtx = VertexList.First;
while (tempVtx.Value.vtx != from)
tempVtx = tempVtx.Next;
EdgeLink tempEdge = new EdgeLink(to);
tempVtx.Value.list.AddLast(tempEdge);
}
public void DeleteEdge(int from, int to)
{
LinkedListNode<Vertex> temp = FindVetex(from);
temp.Value.DeleteEdge(to);
}
public void DeleteVertex(int vtx)
{
LinkedListNode<Vertex> temp = FindVetex(vtx);
VertexList.Remove(temp);
temp = VertexList.First;
while (temp != null)
{
temp.Value.DeleteEdge(vtx);
temp = temp.Next;
}
}
//将图的边和节点输出成string,用来生成dot文件
public string GraphToStr()
{
string s = "digraph g{\n";
s += "\tsize = \"10, 10\";\n";
s += "\tnode [color=lightblue2, style=filled];\n";
string temp1 = "\"";
string temp2 = "->";
string temp3 = ";\n";
LinkedListNode<Vertex> tempVtx = VertexList.First;
LinkedListNode<EdgeLink> tempEdge = tempVtx.Value.list.First;
while (tempVtx != null)
{
tempEdge=tempVtx.Value.list.First;
while (tempEdge != null)
{
s += temp1;
s += tempVtx.Value.vtx;
s += temp1;
s += temp2;
s += temp1;
s += tempEdge.Value.destVtx;
s += temp1;
s += temp3;
tempEdge = tempEdge.Next;
}
tempVtx = tempVtx.Next;
}
s += "}\n\n";
return s;
}
//根据图的string信息,生成dot及jpg文件
public void GraphToJPG(string name)
{
string strname = "";
strname += name;
string dotname = "";
dotname += name;
dotname += ".dot";
string jpgname = "";
jpgname += name;
jpgname += ".jpg";
string dotname2 = dotname;
StreamWriter fp;
fp = File.CreateText(dotname2);
string s = GraphToStr();
fp.WriteLine(s);
fp.Close();
string command = "dot -Tjpg -o ";
command += jpgname;
command += " ";
command += dotname;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
p.StandardOutput.ReadToEnd();
p.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -