地图绘制和四色算法,图搜索算法,最小生成树算法,最短路径算法

基于简易Web墨卡托计算实现地图绘制,四色染色,实现图的深度优先搜索,广度优先搜索,Kruskal算法最小生成树,Prime算法最小生成树,Dijkstra最短路径算法。

使用Java & Swing开发

基于简易Web墨卡托计算,地图绘制

地图绘制和四色算法,图搜索算法,最小生成树算法,最短路径算法_第1张图片

四色定理染色:

	// 四色算法
	public void fill() {
		LinkedList source = (LinkedList)__nodes.clone();
		LinkedList target = new LinkedList<>();
		
		while (source.isEmpty() == false) {
			
			INode node = source.pop();
			target.push(node);
			
			boolean bContinue = false;
			do {
				INode top = target.peek();
				Color c = findColor(top);
				
				if (c == null) {
					INode n = target.pop();
					n.setColor(null);
					source.push(n);
					continue ;
				}
				
				top.setColor(c);
				bContinue = ! detect(top);
				
			} while (bContinue == false);
		}
		
	}

地图绘制和四色算法,图搜索算法,最小生成树算法,最短路径算法_第2张图片

BFS优先搜索算法

	// 广度优先搜索
	public void bfs() {
		LinkedList all = (LinkedList)__nodes.clone();
		LinkedList q = new LinkedList<>();

		while (all.isEmpty() == false) {
			INode node = all.removeFirst();
			q.add(node);

			int p = 0;
			while (p < q.size()) {
				INode n = q.get(p);

				for (INode n1 : n.getChildren()) {
					boolean b = all.remove(n1);
					if (b)
						q.add(n1);
				}

				p++;
			}
		}

		__nodes.clear();
		__nodes.addAll(q);

	}

 地图绘制和四色算法,图搜索算法,最小生成树算法,最短路径算法_第3张图片

DFS深度优先搜索算法:

	// 深度优先搜索
	public void dfs() {
		LinkedList all = (LinkedList)__nodes.clone();
		LinkedList q = new LinkedList<>();
		
		while (all.isEmpty() == false) {
			INode node = all.removeFirst();
			q.add(node);

			_dfs(all, q, node);
		}

		__nodes.clear();
		__nodes.addAll(q);
	}
	
	private void _dfs(LinkedList all, LinkedList target, INode c) {
		for (INode n : c.getChildren()) {
			boolean b = all.remove(n);
			if (b) {
				target.add(n);
				_dfs(all, target, n);
			}
		}
	}

地图绘制和四色算法,图搜索算法,最小生成树算法,最短路径算法_第4张图片

Kruskal最小生成树:

	public List Kruskal() {
		HashSet _edges = buildEdge();

		LinkedList edges = new LinkedList<>();
		edges.addAll(_edges);

		Collections.sort(edges, new Comparator() {
			@Override
			public int compare(Edge o1, Edge o2) {
				return o1.compareTo(o2);
			}
		});

		List> range = new LinkedList<>();
		LinkedList r = new LinkedList<>();

		for (Edge edge : edges) {
			HashSet s1 = null;
			HashSet s2 = null;

			for (HashSet s : range) {
				if (s.contains(edge.n1) == true) {
					s1 = s;
				}
				if (s.contains(edge.n2) == true) {
					s2 = s;
				}
				if (s1 != null && s2 != null)
					break ;
			}

			if (s1 != null && s2 != null) {
				if (s1 == s2)
					continue ;
				else {
					HashSet s = new HashSet<>();
					s.addAll(s1);
					s.addAll(s2);
					range.remove(s1);
					range.remove(s2);
					range.add(s);
					r.add(edge);
				}
			}
			if (s1 == null && s2 == null) {
				HashSet s = new HashSet<>();
				s.add(edge.n1);
				s.add(edge.n2);
				range.add(s);
				r.add(edge);
			}
			if (s1 != null && s2 == null) {
				s1.add(edge.n2);
				r.add(edge);
			}
			if (s1 == null && s2 != null) {
				s2.add(edge.n1);
				r.add(edge);
			}
		}

		return r;
	}

地图绘制和四色算法,图搜索算法,最小生成树算法,最短路径算法_第5张图片

Prime最小生成树

	public List Prime() {
		LinkedList r = new LinkedList<>();
		LinkedList range = new LinkedList<>();

		range.add( this.randomNode() );

		while (true) {

			int min = -1;
			Edge minEdge = null;

			for (INode node : range) {
				for (INode child : node.getChildren()) {
					if (range.contains(child) == false) {
						Edge edge = new Edge(node, child);
						int w = Edge.getDistance(node, child);

						if (minEdge == null) {
							minEdge = edge;
							min = w;
						}
						else if (w < min) {
							minEdge = edge;
							min = w;
						}
					}
				}
			}

			if (minEdge != null) {
				range.add(minEdge.n1);
				range.add(minEdge.n2);
				r.add(minEdge);
			}
			else
				break ;
		}

		return r;
	}

地图绘制和四色算法,图搜索算法,最小生成树算法,最短路径算法_第6张图片

Dijkstra算法

	// Dijkstra算法
	public List Dijkstra() {

		for (int i=0; i<__nodes.size(); i++) {
			__nodes.get(i).setWeight(-1);
		}

		HashSet edges = buildEdge();
		LinkedList r = new LinkedList<>();
		LinkedList s = new LinkedList<>();
		LinkedList all = (LinkedList) __nodes.clone();

		INode starter = randomNode();
		starter.setWeight( 0 );
		s.add(starter);
		all.remove(starter);

		while (all.isEmpty() == false) {

			INode minNode = null;
			int minWeight = -1;
			Edge e = null;

			for (INode n1 : all) {
				for (INode n2 : s) {
					Edge edge = findEdge(edges, n1, n2);

					if (edge != null) {
						int d = n2.getWeight() + edge.getDistance();
						if (minNode == null) {
							minNode = n1;
							minWeight =d;
							e = edge;
						}
						else if (d < minWeight) {
							minNode = n1;
							minWeight = d;
							e = edge;
						}
					}
				}
			}

			if (minNode != null) {
				minNode.setWeight(minWeight);
				s.add(minNode);
				all.remove(minNode);
				r.add(e);
			}
			else
				break;
		}

		return r;
	}

地图绘制和四色算法,图搜索算法,最小生成树算法,最短路径算法_第7张图片

你可能感兴趣的:(程序设计,图算法,Java,数据结构)