`
cy729215495
  • 浏览: 129517 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

graph code

 
阅读更多

 

 

 

package com.tech.cglibx;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

public class MyGraph {
	

public static void main(String[] args) {
		Graph graph = new Graph();
		Set<Vertex> vertexSet = graph.getVertexSet();
		Map<Vertex, Vertex[]> edgeMap = graph.getAdjacencys();

		Vertex twoVertex = new Vertex("2");
		Vertex threeVertex = new Vertex("3");
		Vertex fiveVertex = new Vertex("5");
		Vertex sevenVertex = new Vertex("7");
		Vertex eightVertex = new Vertex("8");
		Vertex nineVertex = new Vertex("9");
		Vertex tenVertex = new Vertex("10");
		Vertex elevenVertex = new Vertex("11");

		vertexSet.add(twoVertex);
		vertexSet.add(threeVertex);
		vertexSet.add(fiveVertex);
		vertexSet.add(sevenVertex);
		vertexSet.add(eightVertex);
		vertexSet.add(nineVertex);
		vertexSet.add(tenVertex);
		vertexSet.add(elevenVertex);

		edgeMap.put(twoVertex, new Vertex[] { elevenVertex });
		edgeMap.put(nineVertex, new Vertex[] { elevenVertex, eightVertex });
		edgeMap.put(tenVertex, new Vertex[] { elevenVertex, threeVertex });
		edgeMap.put(elevenVertex, new Vertex[] { sevenVertex, fiveVertex });
		edgeMap.put(eightVertex, new Vertex[] { sevenVertex, threeVertex });

		topologicalSort2(graph,twoVertex);
		//Vertex[] sortedVertexs = topologicalSort(graph);
		//printVertex(sortedVertexs);
	}

	public static void printVertex(Vertex[] Vertexs) {
		for (Vertex vertex : Vertexs) {
			System.out.println(vertex.getName() + "  discover time:"
					+ vertex.getDiscover() + "  finish time:"
					+ vertex.getFinish());
		}
	}
	
	
	 static class TimeRecorder {
			private int time = 0;

			public int getTime() {
				return time;
			}

			public void setTime(int time) {
				this.time = time;
			}
		}

		public static Vertex[] topologicalSort(Graph graph) {
			Set<Vertex> vertexSet = graph.getVertexSet();
			if (vertexSet.size() < 2) {
				return vertexSet.toArray(new Vertex[0]);
			}

			LinkedList<Vertex> sortedList = new LinkedList<Vertex>();
			TimeRecorder recorder = new TimeRecorder();

			for (Vertex vertex : vertexSet) {
				if (vertex.color == Color.WHITE) {
					visitVertex(graph, vertex, recorder, sortedList);
				}
			}

			return sortedList.toArray(new Vertex[0]);
		}
		
		
		public static void topologicalSort2(Graph graph,Vertex vertex) {

			LinkedList<Vertex> sortedList = new LinkedList<Vertex>();
			TimeRecorder recorder = new TimeRecorder();
		
			 visitVertex(graph, vertex, recorder, sortedList);
			System.out.println(sortedList);
		}

		/**
		 * 深度优先搜索(Depth First Search)
		 */
		public static void visitVertex(Graph graph, Vertex vertex,
				TimeRecorder recorder, LinkedList<Vertex> sortedList) {
			recorder.time += 1;
			vertex.color = Color.GRAY;
			vertex.discover = recorder.time;

			Map<Vertex, Vertex[]> edgeMap = graph.getAdjacencys();
			Vertex[] adjacencys = edgeMap.get(vertex);
			if (adjacencys != null && adjacencys.length > 0) {
				for (Vertex adjacency : adjacencys) {
					if (adjacency.color == Color.WHITE) {
						adjacency.parent = vertex;
						visitVertex(graph, adjacency, recorder, sortedList);
					}
				}
			}

			recorder.time += 1;
			vertex.color = Color.BLACK;
			vertex.finish = recorder.time;
			sortedList.addLast(vertex);
		}
	
	 enum Color {
			WHITE, GRAY, BLACK
		}

		static class Vertex {
			private String name;
			private Color color;
			private Vertex parent;
			private int discover;
			private int finish;
			
			

			@Override
			public String toString() {
				return String.format("Vertex [name=%s, color=%s, parent=%s]", name, color,
						parent);
			}

			public Vertex(String name) {
				this.name = name;
				this.color = Color.WHITE;
			}

			public String getName() {
				return name;
			}

			public void setName(String name) {
				this.name = name;
			}

			public Color getColor() {
				return color;
			}

			public void setColor(Color color) {
				this.color = color;
			}

			public Vertex getParent() {
				return parent;
			}

			public void setParent(Vertex parent) {
				this.parent = parent;
			}

			public int getDiscover() {
				return discover;
			}

			public void setDiscover(int discover) {
				this.discover = discover;
			}

			public int getFinish() {
				return finish;
			}

			public void setFinish(int finish) {
				this.finish = finish;
			}

			@Override
			public int hashCode() {
				final int prime = 31;
				int result = 1;
				result = prime * result + ((name == null) ? 0 : name.hashCode());
				return result;
			}

			@Override
			public boolean equals(Object obj) {
				if (this == obj)
					return true;
				if (obj == null)
					return false;
				if (getClass() != obj.getClass())
					return false;
				Vertex other = (Vertex) obj;
				if (name == null) {
					if (other.name != null)
						return false;
				} else if (!name.equals(other.name))
					return false;
				return true;
			}
		}

		static class Graph {
			private Set<Vertex> vertexSet = new HashSet<Vertex>();
			// 相邻的节点
			private Map<Vertex, Vertex[]> adjacencys = new HashMap<Vertex, Vertex[]>();

			public Set<Vertex> getVertexSet() {
				return vertexSet;
			}

			public void setVertexSet(Set<Vertex> vertexSet) {
				this.vertexSet = vertexSet;
			}

			public Map<Vertex, Vertex[]> getAdjacencys() {
				return adjacencys;
			}

			public void setAdjacencys(Map<Vertex, Vertex[]> adjacencys) {
				this.adjacencys = adjacencys;
			}
		}
}

 

分享到:
评论

相关推荐

    CODE_MAP.rar_CODE_MAP.rar_graph cut_graph cut CODE_M_graphcut_立体

    《Graph Cut算法详解及其在CODE_MAP中的应用》 在计算机科学和图像处理领域,Graph Cut算法是一种被广泛应用的优化技术,特别是在图像分割和立体视觉中。本文将详细解析Graph Cut算法的基本原理,并结合CODE_MAP的...

    graph coloring code and website

    some websites for graph coloring and joe cuberson's graph coloring and graph generator in gnu c++ in unix and a very smart c++ source code for vertex coloring in vc++.

    code.zip_code graph_matlab code_saliency_saliency matlab_visual

    Graph-Based Visual Saliency (MATLAB source code)

    graph cut code

    在这个"graph cut code"的压缩包中,我们可以看到一系列与实现该算法相关的文件。 1. **waterfall.bmp**:这可能是一个示例图像文件,用于测试Graph Cut算法。`bmp`是常见的位图图像格式,通常用于存储未经压缩的...

    Seg.zip_Graph Cut image_In the Cut_graph cut_graph cut code_grap

    "Seg.zip_Graph Cut image_In the Cut_graph cut_graph cut code_grap" 这个压缩包包含了一个关于使用图割(Graph Cut)算法进行图像分割的项目。图割是一种优化方法,常用于计算机视觉和图像分析,它将图像像素之间...

    CCS Graph观察变量的方法.

    Code Composer Studio (CCS) 是一款广泛应用于TI处理器的集成开发环境(IDE),它提供了强大的调试功能,包括使用CCS Graph来观察变量的变化趋势。本文将以直观且具体的方式介绍如何使用CCS Graph观察变量的方法,帮助...

    g2o实践GraphSLAM_tutorials_code代码

    - **关键文件**:"GraphSLAM_tutorials_code修改版"和原始的"GraphSLAM_tutorials_code"文件夹,可能包含了解决编译问题的修改,这为初学者提供了参考。 4. **编译与运行** - **依赖库**:编译g2o和GraphSLAM_...

    CCS5.5 Graph 使用总结

    TI公司的Code Composer Studio (CCS) 是一个强大的集成开发环境,特别针对TI的数字信号处理器(DSP)设计。CCS 5.5版本引入了Graph功能,为开发者提供了图形化的数据观察和分析工具,这对于理解和优化代码性能至关重要...

    GraphCut_matlab.rar_code matlab_experiencewbk_graph cut_graph-cu

    graph cut segmentation method.

    Machine learning for graph-based representations

    characterizing transport in DFNs, by combining graph theoretical and ma- chine learning methods. We consider a graph representation where nodes sig- nify fractures and edges denote their intersections...

    CCS中的graph详细使用说明

    CCS(Code Composer Studio)是一款由Texas Instruments(TI)开发的强大集成开发环境(IDE),主要用于TI的数字信号处理器(DSP)和其他微控制器的软件开发。其中,Graph功能是CCS提供的一项非常重要的可视化工具,可以帮助...

    VB.rar_VB 控件_ds1820 graph vb code_vb 界面

    "ds1820 graph vb code" 提示我们,这个项目可能涉及到通过VB编程读取并显示ds1820温度传感器的数据,并且这部分数据被图形化展示。"vb 界面"则意味着此项目着重于创建用户界面,以提供友好的交互体验。 【描述】中...

    graph and chart1.22

    All charts are completly customizable and can be set up quickly either from code or from the unity editor. Graph And Chart can be used with any platform including VR/AR, mobile ,web and desktop. All ...

    CCS3.3中的graph详细使用说明

    在CCS(Code Composer Studio)3.3版本中,提供了强大的图形显示功能,能够帮助开发者直观地查看和分析信号处理的结果。这些图形功能覆盖了多种类型的数据可视化,如时频分析、星座图、眼图以及图像显示等。本文将详细...

    Graph_Fusion_Code_Data.zip

    本资料“Graph_Fusion_Code_Data.zip”主要聚焦于一种创新的图像检索策略——全局特征与局部特征的融合,以此提升检索的准确性和鲁棒性。 全局特征通常是从整个图像中提取的、能够概括图像主要内容的信息。例如,...

    unity数据分析图表Graph And Chart 1.2

    Completely customizable from the editor and does not require even one line of code. Can be used within any platform including VR/AR. Bar Chart , Pie Chart , Torus Chart , Graph Chart , Bubble Chart ...

    Code2Graph:致力于将源代码转换为原子图表示

    Code2Graph:将源代码转换为图形。 通过对嵌套层次结构,控制和数据流的轻量级静态分析。 对于软件和语言工程中的各种下游任务: 软件架构分析 语义代码差异 语义代码合并 对代码更改的影响分析 不同粒度下的协同...

    graph_algo_pesudo_code.txt.bak

    graph_algo_pesudo_code.txt.bak

Global site tag (gtag.js) - Google Analytics