1.图的结构

package;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class GraphGenerator {
	public static class Graph{
		HashMap<Integer,Node> nodes;
		HashSet<Edge> edges;
		public Graph(){
			nodes=new HashMap<>();
			edges=new HashSet<>();
		}
	}
	public static class Node{
		int value;
		int in;
		int out;
		ArrayList<Node> nexts;
		ArrayList<Edge> edges;
		public Node(int value) {
			this.value=value;
			in=0;
			out=0;
			nexts=new ArrayList<>();
			edges=new ArrayList<>();
		}
	}
	public static class Edge{
		int weight;
		Node from;
		Node to;
		public Edge(int weight,Node from,Node to) {
			this.weight=weight;
			this.from=from;
			this.to=to;
		}
	}
	public static Graph createGraph(Integer[][] matrix) {
		Graph graph=new Graph();
		for(int i=0;i<matrix.length;i++) {
			Integer weight=matrix[i][0];
			Integer from=matrix[i][1];
			Integer to=matrix[i][2];
			if(!graph.nodes.containsKey(from)) {
				graph.nodes.put(from,new Node(from));
			}if(!graph.nodes.containsKey(to)) {
				graph.nodes.put(to,new Node(to));
			}
			Node fromNode=graph.nodes.get(from);
			Node toNode=graph.nodes.get(to);
			Edge newEdge=new Edge(weight,fromNode,toNode);
			fromNode.nexts.add(toNode);
			fromNode.out++;
			toNode.in++;
			fromNode.edges.add(newEdge);
			graph.edges.add(newEdge);
		}
		return graph;
	}
}

2.图的宽度遍历

package;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;

public class BFS {
	public static class Graph{
		HashMap<Integer,Node> nodes;
		HashSet<Edge> edges;
		public Graph() {
			nodes=new HashMap<>();
			edges=new HashSet<>();
		}
	}
	public static class Node{
		int value;
		ArrayList<Node> nexts;
		ArrayList<Edge> edges;
		int in;
		int out;
		public Node(int value) {
			this.value=value;
		}
	}
	public static class Edge{
		int weight;
		Node from;
		Node to;
		public Edge(int weight,Node from,Node to) {
			this.weight=weight;
			this.from=from;
			this.to=to;
		}
	}
	public static void bfs(Node node) {
		if(node==null) {
			return ;
		}
		Queue<Node> queue=new LinkedList<>();
		HashSet<Node> map=new HashSet<>();
		queue.add(node);
		map.add(node);
		while(!queue.isEmpty()) {
			Node cur=queue.poll();
			System.out.println(cur.value);
			for(Node next:cur.nexts) {
				if(!map.contains(next)) {
					map.add(next);
					queue.add(next);
				}
			}
		}
	}
}

3.图的深度遍历

package;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Stack;


public class DFS {
	public static class Graph{
		HashMap<Integer,Node> nodes;
		HashSet<Edge> edges;
		public Graph() {
			nodes=new HashMap<>();
			edges=new HashSet<>();
		}
	}
	public static class Node{
		int value;
		ArrayList<Node> nexts;
		ArrayList<Edge> edges;
		int in;
		int out;
		public Node(int value) {
			this.value=value;
		}
	}
	public static class Edge{
		int weight;
		Node from;
		Node to;
		public Edge(int weight,Node from,Node to) {
			this.weight=weight;
			this.from=from;
			this.to=to;
		}
	}
	public static void dfs(Node node) {
		if(node==null) {
			return;
		}
		Stack<Node> stack=new Stack<>();
		HashSet<Node> set=new HashSet<>();
		stack.add(node);
		set.add(node);
		System.out.println(node.value);
		while(!stack.isEmpty()) {
			Node cur=stack.pop();
			for(Node next:cur.nexts) {
				if(!set.contains(next)) {
					stack.push(next);
					set.add(next);
					System.out.println(next.value);
					break;
				}
			}
		}
	}
}

4.拓扑排序

package exercise;

import java.util.*;
public class sortedTopology {
	public static class Graph{
		HashMap<Integer,Node> nodes;
		HashSet<Edge> edges;
		public Graph() {
			nodes=new HashMap<>();
			edges=new HashSet<>();
		}
	}
	public static class Node{
		int value;
		ArrayList<Node> nexts;
		ArrayList<Edge> edges;
		int in;
		int out;
		public Node(int value) {
			this.value=value;
		}
	}
	public static class Edge{
		int weight;
		Node from;
		Node to;
		public Edge(int weight,Node from,Node to) {
			this.weight=weight;
			this.from=from;
			this.to=to;
		}
	}
	public static List<Node> sortedTopology(Graph graph){
		HashMap<Node,Integer> inMap=new HashMap<>();
		Queue<Node> zeroInQueue=new LinkedList<>();
		for(Node node:graph.nodes.values()) {
			inMap.put(node, node.in);
			if(node.in==0) {
				zeroInQueue.add(node);
			}
		}
		List<Node> result=new ArrayList<>();
		while(!zeroInQueue.isEmpty()) {
			Node cur=zeroInQueue.poll();
			result.add(cur);
			for(Node next:cur.nexts) {
				inMap.put(next,inMap.get(next)-1);
				if(inMap.get(next)==0) {
					zeroInQueue.add(next);
				}
			}
		}
		return result;
	}
}

5.Kruskal算法

package exercise;

import java.util.*;
public class Kruskal {
	public static class Graph{
		HashMap<Integer,Node> nodes;
		HashSet<Edge> edges;
		public Graph() {
			nodes=new HashMap<>();
			edges=new HashSet<>();
		}
	}
	public static class Node{
		int value;
		ArrayList<Node> nexts;
		ArrayList<Edge> edges;
		int in;
		int out;
		public Node(int value) {
			this.value=value;
		}
	}
	public static class Edge{
		int weight;
		Node from;
		Node to;
		public Edge(int weight,Node from,Node to) {
			this.weight=weight;
			this.from=from;
			this.to=to;
		}
	}
	public static class UnionFind{
		public HashMap<Node,Node> fatherMap;
		public HashMap<Node,Integer> rankMap;
		public UnionFind() {
			fatherMap=new HashMap<Node,Node>();
			rankMap=new HashMap<Node,Integer>();
		}
		public Node findFather(Node n) {
			Node father=fatherMap.get(n);
			if(father!=n) {
				father=findFather(father);
			}
			fatherMap.put(n,father);
			return father;
		}
		public void makeSets(Collection<Node> nodes) {
			fatherMap.clear();
			rankMap.clear();
			for(Node node:nodes) {
				fatherMap.put(node, node);
				rankMap.put(node, 1);
			}
		}
		public boolean isSameSet(Node a,Node b) {
			return findFather(a)==findFather(b);
		}
		public void union(Node a,Node b) {
			if(a==null||b==null) {
				return;
			}
			Node aFather=findFather(a);
			Node bFather=findFather(b);
			if(aFather!=bFather) {
				int aFrank=rankMap.get(aFather);
				int bFrank=rankMap.get(bFather);
				if(aFrank<=bFrank) {
					fatherMap.put(aFather,bFather);
					rankMap.put(bFather, aFrank+bFrank);
				}
				else {
					fatherMap.put(bFather,aFather);
					rankMap.put(aFather,aFrank+bFrank);
				}
			}
		}
	}
	public static class EdgeComparator implements Comparator<Edge>{
		public int compare(Edge o1,Edge o2) {
			return o1.weight-o2.weight;
		}
	}
	public static Set<Edge> KruskalMST(Graph graph){
		UnionFind unionFind=new UnionFind();
		unionFind.makeSets(graph.nodes.values());
		PriorityQueue<Edge> priorityQueue=new PriorityQueue<>(new EdgeComparator());
		for(Edge edge:graph.edges) {
			priorityQueue.add(edge);
		}
		Set<Edge> result=new HashSet<>();
		while(!priorityQueue.isEmpty()) {
			Edge edge=priorityQueue.poll();
			if(!unionFind.isSameSet(edge.from, edge.to)) {
				result.add(edge);
				unionFind.union(edge.from,edge.to);
			}
		}
		return result;
	}
}

6.Prim算法

package exercise;

import java.util.*;
public class Prim {
	public static class Graph{
		HashMap<Integer,Node> nodes;
		HashSet<Edge> edges;
		public Graph() {
			nodes=new HashMap<>();
			edges=new HashSet<>();
		}
	}
	public static class Node{
		int value;
		ArrayList<Node> nexts;
		ArrayList<Edge> edges;
		int in;
		int out;
		public Node(int value) {
			this.value=value;
		}
	}
	public static class Edge{
		int weight;
		Node from;
		Node to;
		public Edge(int weight,Node from,Node to) {
			this.weight=weight;
			this.from=from;
			this.to=to;
		}
	}
	public static class EdgeComparator implements Comparator<Edge>{
		public int compare(Edge o1,Edge o2) {
			return o1.weight-o2.weight;
		}
	}
	public static Set<Edge> primMST(Graph graph){
		PriorityQueue<Edge> priorityQueue=new PriorityQueue<>(new EdgeComparator());
		HashSet<Node> set=new HashSet<>();
		Set<Edge> result=new HashSet<>();
		for(Node node:graph.nodes.values()) {
			if(!set.contains(node)) {
				set.add(node);
				for(Edge edge:node.edges) {
					priorityQueue.add(edge);
				}
				while(!priorityQueue.isEmpty()) {
					Edge edge=priorityQueue.poll();
					Node toNode=edge.to;
					if(!set.contains(toNode)) {
						set.add(toNode);
						result.add(edge);
						for(Edge nextEdge:toNode.edges) {
							priorityQueue.add(nextEdge);
						}
					}
				}
			}
		}
		return result;
	}
}

7.迪杰特斯拉算法

package;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;

public class Dijkstra {
	public static class Graph{
		HashMap<Integer,Node> nodes;
		HashSet<Edge> edges;
		public Graph(){
			nodes=new HashMap<>();
			edges=new HashSet<>();
		}
	}
	public static class Node{
		int value;
		int in;
		int out;
		ArrayList<Node> nexts;
		ArrayList<Edge> edges;
		public Node(int value) {
			this.value=value;
			in=0;
			out=0;
			nexts=new ArrayList<>();
			edges=new ArrayList<>();
		}
	}
	public static class Edge{
		int weight;
		Node from;
		Node to;
		public Edge(int weight,Node from,Node to) {
			this.weight=weight;
			this.from=from;
			this.to=to;
		}
	}
	public static HashMap<Node,Integer> dijkstra1(Node head){
		HashMap<Node,Integer>distanceMap=new HashMap<>();
		distanceMap.put(head, 0);
		HashSet<Node> selectedNodes=new HashSet<>();
		Node minNode=getMinDistanceAndUnselectedNode(distanceMap,selectedNodes);
		while(minNode!=null) {
			int distance=distanceMap.get(minNode);
			for(Edge edge:minNode.edges) {
				Node toNode=edge.to;
				if(!distanceMap.containsKey(toNode)) {
					distanceMap.put(toNode, distance+edge.weight);
				}
				distanceMap.put(edge.to, Math.min(distanceMap.get(toNode), distance+edge.weight));;
			}
			selectedNodes.add(minNode);
			minNode=getMinDistanceAndUnselectedNode(distanceMap,selectedNodes);
		}
		return distanceMap;
		
	}
	public static Node getMinDistanceAndUnselectedNode(HashMap<Node,Integer> distanceMap,HashSet<Node> touchedNodes) {
		Node minNode=null;
		int minDistance=Integer.MAX_VALUE;
		for(Entry<Node, Integer> entry:distanceMap.entrySet()) {
			Node node=entry.getKey();
			int distance=entry.getValue();
			if(!touchedNodes.contains(node)&&distance<minDistance) {
				minNode=node;
				minDistance=distance;
			}
		}
		return minNode;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值