Interface

AddVertex(T data)

AddEdge(int from, int to)

DFS

BFS

MST

TopSort

PrintGraph


using System;
using System.Collections.Generic;
using System.Linq; namespace TestCase1.TestCase.GraphMatrix
{
public class Vertex<T>
{
public T Data
{
get;
set;
} public bool Visited
{
get;
set;
} public Vertex(T data)
{
this.Data = data;
this.Visited = false;
}
} public class GraphMatrix<T>
{
private int GraphSize = ; private List<Vertex<T>> Vertices
{
get;
set;
} private int[,] adjMatrix
{
get;
set;
} private int Capacity
{
get;
set;
} public GraphMatrix(int capacity)
{
this.Vertices = new List<Vertex<T>>();
this.Capacity = capacity;
this.adjMatrix = new int[capacity, capacity];
for (int i = ; i < capacity; i++)
{
for (int j = ; j < capacity; j++)
{
this.adjMatrix[i, j] = ;
}
}
} public void AddVertex(T data)
{
// ? need check
var result = this.Vertices.Where(t => t.Data.Equals(data)); if (result == null || !result.Any())
{
this.Vertices.Add(new Vertex<T>(data));
this.GraphSize++;
}
} public void AddEdge(int from, int to)
{
if (from >= this.GraphSize || to >= this.GraphSize)
{
throw new ArgumentException("index is out of graph size!");
} this.adjMatrix[from, to] = ;
this.adjMatrix[to, from] = ;
} public void AddDirectedEdge(int from, int to)
{
if (from >= this.GraphSize || to >= this.GraphSize)
{
throw new ArgumentException("index is out of graph size!");
} this.adjMatrix[from, to] = ;
} public void ShowVertex(Vertex<T> ver)
{
Console.WriteLine("Show vertext = {0}", ver.Data);
} public void InitVisit()
{
foreach (var v in this.Vertices)
{
v.Visited = false;
}
} public void PrintGraph()
{
for (int i = ; i < this.GraphSize; i++)
{
Console.WriteLine();
Console.Write("Vertex is {0}: ", this.Vertices[i].Data);
for (int j = ; j < this.GraphSize; j++)
{
if (this.adjMatrix[i, j] == )
{
Console.Write(this.Vertices[j].Data);
Console.Write(" ");
}
}
}
} public int FindVertexWithoutSuccssor()
{
for (int i = ; i < this.GraphSize; i++)
{
bool hasSuccessor = false;
for (int j = ; j < this.GraphSize; j++)
{
if (this.adjMatrix[i, j] == )
{
hasSuccessor = true;
break;
}
} if (!hasSuccessor)
{
return i;
}
} return -;
} public void MoveColumn(int column)
{
for (int row = ; row < this.GraphSize; row++)
{
for (int j = column + ; j < this.GraphSize; j++)
{
this.adjMatrix[row, j - ] = this.adjMatrix[row, j];
}
}
} public void MoveRow(int row)
{
for (int column = ; column < this.GraphSize; column++)
{
for (int j = row + ; j < this.GraphSize; j++)
{
this.adjMatrix[j - , column] = this.adjMatrix[j, column];
}
}
} public void RemoveVertex(int index)
{
this.Vertices.RemoveAt(index);
this.GraphSize--; // important here.
} public void TopSort()
{
Stack<Vertex<T>> stack = new Stack<Vertex<T>>(); while (this.GraphSize > )
{
int vertex = FindVertexWithoutSuccssor();
if (vertex == -)
{
throw new Exception("The graph has cycle!");
} stack.Push(this.Vertices[vertex]); this.MoveRow(vertex);
this.MoveColumn(vertex);
this.RemoveVertex(vertex);
} while (stack.Count != )
{
var ret = stack.Pop();
Console.WriteLine(ret.Data);
}
} public void DFS()
{
Stack<int> stack = new Stack<int>(); // validation
if (this.GraphSize == )
{
Console.WriteLine("graph is empty, no op!");
return;
} stack.Push();
this.Vertices[].Visited = true;
ShowVertex(this.Vertices[]); while (stack.Count != )
{
int index = stack.Peek(); // find next un-visited edge
int v = this.GetNextUnVisitedAdjancentNode(index);
if (v == -)
{
stack.Pop();
}
else
{
this.ShowVertex(this.Vertices[v]);
this.Vertices[v].Visited = true;
stack.Push(v);
}
} // reset ALL VISIT flags
this.InitVisit();
} public void BFS()
{
Queue<int> queue = new Queue<int>(); // validation
if (this.GraphSize == )
{
return;
} // logic
queue.Enqueue();
ShowVertex(this.Vertices[]);
this.Vertices[].Visited = true; while (queue.Count > )
{
int result = queue.Dequeue(); // find adjacent nodes and enqueue them
for (int j = ; j < this.GraphSize; j++)
{
if (adjMatrix[result, j] == && this.Vertices[j].Visited == false)
{
// print all adjacent nodes
ShowVertex(this.Vertices[j]);
this.Vertices[j].Visited = true; queue.Enqueue(j);
}
}
} // reset
this.InitVisit();
} public void MST()
{
// validation
if (this.GraphSize == )
{
return;
} // init
Stack<int> stack = new Stack<int>();
stack.Push();
int currentVertex = ;
int vertex = ; this.Vertices[].Visited = true; while (stack.Count > )
{
currentVertex = stack.Peek(); vertex = this.GetNextUnVisitedAdjancentNode(currentVertex);
if (vertex == -)
{
stack.Pop();
}
else
{
this.Vertices[vertex].Visited = true; // print
Console.Write(this.Vertices[currentVertex].Data.ToString() + this.Vertices[vertex].Data.ToString());
Console.Write("-> ");
stack.Push(vertex);
}
} // clean up
this.InitVisit();
} private int GetNextUnVisitedAdjancentNode(int v)
{
// validation
if (v >= this.GraphSize)
{
throw new Exception("v is out of graph size!");
} for (int i = ; i < this.GraphSize; i++)
{
if (adjMatrix[v, i] == && !this.Vertices[i].Visited)
{
return i;
}
} return -;
} public void DepthFirstSearch()
{
DFSUtil();
this.InitVisit();
} private void DFSUtil(int vertex)
{
// validation
if (vertex >= this.GraphSize)
{
throw new ArgumentException("out of graph size!");
} int ver = this.GetNextUnVisitedAdjancentNode(vertex);
if (ver == -)
{
return;
}
else
{
// print current node
ShowVertex(this.Vertices[ver]);
this.Vertices[ver].Visited = true; DFSUtil(ver);
}
}
}
}

Adjacency matrix based Graph的更多相关文章

  1. Convert Adjacency matrix into edgelist

    Convert Adjacency matrix into edgelist import numpy as np #read matrix without head. a = np.loadtxt( ...

  2. 路径规划 Adjacency matrix 传球问题

    建模 问题是什么 知道了问题是什么答案就ok了 重复考虑 与 重复计算 程序可以重复考虑  但往目标篮子中放入时,放不放把握好就ok了. 集合 交集 并集 w 路径规划 字符串处理 42423 424 ...

  3. 论文解读SDCN《Structural Deep Clustering Network》

    前言 主体思想:深度聚类需要考虑数据内在信息以及结构信息. 考虑自身信息采用 基础的 Autoencoder ,考虑结构信息采用 GCN. 1.介绍 在现实中,将结构信息集成到深度聚类中通常需要解决以 ...

  4. Paper: A Novel Time Series Forecasting Method Based on Fuzzy Visibility Graph

    Problem define a fuzzy visibility graph (undirected weighted graph), then give a new similarity meas ...

  5. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  6. 论文解读(GraRep)《GraRep: Learning Graph Representations with Global Structural Information》

    论文题目:<GraRep: Learning Graph Representations with Global Structural Information>发表时间:  CIKM论文作 ...

  7. UVa 10720 - Graph Construction(Havel-Hakimi定理)

    题目链接: 传送门 Graph Construction Time Limit: 3000MS     Memory Limit: 65536K Description Graph is a coll ...

  8. UVA 10720 Graph Construction 贪心+优先队列

    题目链接: 题目 Graph Construction Time limit: 3.000 seconds 问题描述 Graph is a collection of edges E and vert ...

  9. 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法

    Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. javascript高级程序设计第3版——第8章 BOM(浏览器对象模型)

    第八章,浏览器对象模型 主要介绍了window的几个对象以及框架,窗口的关系,各个浏览器对象的属性以及方法:

  2. 用servlet验证密码2

    function createXMLHttpRequest() { var XMLHttpRequest1; if (window.XMLHttpRequest) { XMLHttpRequest_t ...

  3. Ubuntu连网的问题

    Ubuntu一直提示网络offline,disconnection 首先,进入了无线网络,进入属性,允许其他网络用户通过此计算机的Internet连接: 但是虚拟机仍显示网络未连接:(不知道此步骤是不 ...

  4. Tsinghua 2018 DSA PA2简要题解

    反正没时间写,先把简要题解(嘴巴A题)都给他写了记录一下. upd:任务倒是完成了,我也自闭了. CST2018 2-1 Meteorites: 乘法版的石子合并,堆 + 高精度. 写起来有点烦貌似. ...

  5. learning makefile set debug level and build command

  6. python正则表达式补充

    import re origin= "hello alex bcd alex 1ge alex acd 19" r=re.match("(?P<n1>h)(? ...

  7. Oracle入门知识

    在客户端里PL/sql里面 记得用commint 回滚 所写得SQL语句才真的有效  如插入7千万个数据 没有执行commint 就等于没有 将数据真正的存入数据库服务器里面去 所以当其他前端链接上 ...

  8. Metasploit模块简述

    辅助模块.渗透攻击模块.后渗透攻击模块.攻击载荷模块.空指令模块.编码器模块 做了一个思维导图,方便理解. 有需要的就下载吧: 链接:https://share.weiyun.com/5e4XVa1 ...

  9. Centos 配置mailx使用外部smtp发送邮件

    安装mailx yum install mailx 配置mailx 笔者推荐163邮箱,当然,QQ邮箱也是可以的,PS:记得要进邮箱打开SMTP vi /etc/mail.rc //如果不存在,则编辑 ...

  10. [Codeforces375E]Red and Black Tree

    Problem 给定一棵有边权的树.树上每个点是黑或白的.黑白点能两两交换. 求符合任意一个白点到最近黑点的距离小于等于x时,黑白点交换次数最少为多少. Solution 明显是一题树形DP.我们先跑 ...