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. C++标准模板库(STL)之String

    1.String的常用用法 在C语言中,使用字符数组char str[]来存字符串,字符数组操作比较麻烦,而且容易有'\0'的问题,C++在STL中加入string类型,对字符串常用的需求功能进行封装 ...

  2. spiflash

    1.SPI Flash (即SPI Nor Flash)是Nor Flash的一种:2.NOR Flash根据数据传输的位数可以分为并行(Parallel)NOR Flash和串行(SPI)NOR F ...

  3. 【转】使用iTextSharp在Asp.Net中操作PDF

    使用iTextSharp在Asp.Net中操作PDF操作 使用iTextSharp在Asp.Net中操作PDF系列文章目录 实战 iTextSharp iTextSharp 合并多个PDF文件 C#生 ...

  4. C#或unity中实现正弦函数

    C#或unity中实现正弦函数 本类用于第一,需要绘制一条正弦曲线的朋友:第二,需要根据正弦曲线控制物体运动的朋友:里面都有注释,代码如下: unity中使用的代码: public class Cur ...

  5. APP包打包签名步骤

    开发混合app上架应用市场,需要进行应用签名,但是申请签名如果没搞过,会特别麻烦,所以我自自己总结了一下申请的步骤,在此记录一下 1.首先需要下载安装java环境即jdk, 2.配置环境变量 假设JD ...

  6. Metasploit模块简述

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

  7. How to find out which version of tensorflow is installed in my pc? - 如何找出我的电脑中安装了哪个版本的 tensorflow?

    I'm using tensorflow and have got some weired issues. I'm told it might be related to the version of ...

  8. python机器可读数据-csv

    逗号分隔值(Comma-Separated Values,CSV) 1 csv数据 还有一种数据类型,叫制表分隔值(tab-separated values,TSV)数据,有时与CSV归为一类. 若文 ...

  9. 文件防删除保护(miniifiter)

    0x01 思路(原理) 驱动层文件保护的思路是通过minifilter过滤文件删除相关的IRP,并将目标文件与被保护文件相比较,命中保护规则的话返回STATUS_ACCESS_DENIED拒绝访问:也 ...

  10. 关于Apache的配置方法和步骤

    一.下载.安装和卸载 网址:https://httpd.apache.org/docs/current/platform/windows.html#down 点击ApacheHaus,在里面下载任何版 ...