You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package class16 ;
public class GraphGenerator {
// matrix 所有的边
// N*3 的矩阵
// [weight, from节点上面的值, to节点上面的值]
//
// [ 5 , 0 , 7]
// [ 3 , 0, 1]
//
public static Graph createGraph ( int [ ] [ ] matrix ) {
Graph graph = new Graph ( ) ;
for ( int i = 0 ; i < matrix . length ; i + + ) {
// 拿到每一条边, matrix[i]
int weight = matrix [ i ] [ 0 ] ;
int from = matrix [ i ] [ 1 ] ;
int 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 ;
}
}