Exemple #1
0
		// ----------------------KRUSKAL--------------------------------------------

		public Graph kruskal() {
			Edge edge;
			Graph result = new Graph();
			result.setDirected(directed);
			result.addVertex(this.getVertices()[0].getName()); //adding the first vertex

			for (int i = 0; i < getEdges().Count; i++) {
				// look for the unvisited edge with the lower weight
				edge = lowerWeight();
				String start = edge.getStart().getName();
				String end = edge.getEnd().getName();
				int size = result.getVertices().Count;
				// if the edge do not create a cycle and one of the vertices is not in the result graph, it is added to the Kruskal's graph
				// Tree (or forest)
				if ((!result.hasCycle(edge)) &&
					((result.vertexLocation(start)==size)||(result.vertexLocation(end)==size)||(result.isConnected()!=isConnected()))){
					result.addEdge(edge.getWeight(), start, end);
				}
			}

			foreach (Vertex v in vertices){
				if(result.vertexLocation(v.getName())==result.getVertices().Count||!result.isConnected()){
					result.addVertex(v.getName());
				}
			}

			return result;
		}
		public void displayGraph(){
			List<Vertex> vertices;
			Graph graphAux = new Graph();

			switch (algorithm){
			case 2:
				graphAux = graph;
				bDescription.Enabled = false;
				break;
			case 3:
				graphAux = graph.kruskal();
				break;
			case 4:
				graphAux = graph.dijkstra(start,end);
				break;
			case 5:
				graphAux = graph.breadthFirstSearch(start);
				break;
			case 6:
				graphAux = graph.depthFirstSearch(start);
				break;
			case 8:
				graphAux = graph.transitiveClosure();
				break;
			}
			vertices = graphAux.getVertices();

			for(int i=0;i<vertices.Count;i++){
				tvVertices[i].Text = vertices[i].getName();
				tvVertices[i].Visibility = ViewStates.Visible;
			}	

			for(int i=0;i<vertices.Count;i++) {
				for (int j = 0; j < vertices.Count; j++) {
					Edge edge = graphAux.findEdge(vertices[i], vertices[j]);
					if (edge != null) {

						tvEdges[i,j].Text = (edge.getWeight() +""); // using ' +"" ' as a easy form of conversion from int to String
						tvEdges[i,j].Visibility = ViewStates.Visible;
					}
				}
			}

			tvGraph.Text = graphAux.printGraph();

		}
Exemple #3
0
		public Graph graphCreator (List<Edge> pathEdge){
			Graph graph = new Graph();
			graph.setDirected(directed);

			foreach (Edge e in pathEdge)
				graph.addEdge(e.getWeight(), e.getStart().getName(), e.getEnd().getName());


			return graph;
		}
		public static void initiate(Intent i){
			graph = new Graph();
			graph.setDirected(i.GetBooleanExtra("directed", false));
			if (i.GetBooleanExtra("random", false))
				graph.randomGraphCreator();
		}