图
1. 图的表示方法
2. 推荐图的表达
public class Graph {
public HashMap< Integer, Node> nodes;
public HashSet< Edge> edges;
public Graph ( ) {
this . nodes = new HashMap < > ( ) ;
this . edges = new HashSet < > ( ) ;
}
}
public class Edge {
public int weight;
public Node from;
public Node to;
public Edge ( int weight, Node from, Node to) {
this . weight = weight;
this . from = from;
this . to = to;
}
}
public class Node {
public int value;
public int in;
public int out;
public ArrayList< Node> nexts;
public ArrayList< Edge> edges;
public Node ( int value) {
this . value = value;
in = 0 ;
out = 0 ;
nexts = new ArrayList < > ( ) ;
edges = new ArrayList < > ( ) ;
}
}
public static Graph createGraph ( Integer[ ] [ ] matrix) {
Graph graph = new Graph ( ) ;
for ( int i = 0 ; i < matrix. length; i++ ) {
Integer from = matrix[ i] [ 0 ] ;
Integer to = matrix[ i] [ 1 ] ;
Integer weigth = 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 ( weigth, fromNode, toNode) ;
fromNode. nexts. add ( toNode) ;
fromNode. out++ ;
toNode. in++ ;
fromNode. edges. add ( newEdge) ;
graph. edges. add ( newEdge) ;
}
return graph;
}
public static void bfs ( Node node) {
if ( node == null) {
return ;
}
Queue< Node> queue = new LinkedList < > ( ) ;
HashSet< Node> set = new HashSet < > ( ) ;
queue. add ( node) ;
set. add ( node) ;
while ( ! queue. isEmpty ( ) ) {
Node cur = queue. poll ( ) ;
System. out. println ( cur. value) ;
for ( Node next : cur. nexts) {
if ( ! set. contains ( next) ) {
set. add ( next) ;
queue. add ( next) ;
}
}
}
}
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 ( cur) ;
stack. push ( next) ;
set. add ( next) ;
System. out. println ( next. value) ;
break ;
}
}
}
}
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;
}
public static class EdgeComparator implements Comparator < Edge> {
@Override
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 ( ) ) ;
Set< 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;
}
public static HashMap< Node, Integer> dijkstra1 ( Node head) {
HashMap< Node, Integer> distanceMap = new HashMap < > ( ) ;
distanceMap. put ( head, 0 ) ;
HashSet< Node> selectNodes = new HashSet < > ( ) ;
Node minNode = getMinDistanceAndUnSelectedNode ( distanceMap, selectNodes) ;
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) ) ;
}
selectNodes. add ( minNode) ;
minNode = getMinDistanceAndUnSelectedNode ( distanceMap, selectNodes) ;
}
return distanceMap;
}
private static Node getMinDistanceAndUnSelectedNode ( HashMap< Node, Integer> distanceMap, HashSet< Node> touchedNodes) {
Node minNode = null;
int minDistance = Integer. MAX_VALUE;
for ( Map. 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;
}