Graph.h
# pragma once
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# include <string.h>
# include <stdbool.h>
# include <limits.h>
# define MAX 100
# define INF INT_MAX
typedef struct Graph
{ int n; char vertex[ MAX] ; int edges[ MAX] [ MAX] ;
} Graph;
int get_pos ( Graph* g, char ch) ;
void CreateGraph ( Graph* & G, int n) ;
void PrintGraph ( Graph * g) ;
void DestroyGraph ( Graph* g) ;
functions.cpp
# include "Graph.h"
int get_pos ( Graph* g, char ch)
{ for ( int i = 0 ; i < g-> n; i++ ) { if ( ch== g-> vertex[ i] ) { return i; } } return - 1 ;
}
void CreateGraph ( Graph* & G, int n)
{ G= ( Graph* ) malloc ( sizeof ( Graph) ) ; assert ( G) ; G-> n = n; for ( int i = 0 ; i < G-> n; i++ ) { for ( int j = 0 ; j < G-> n; j++ ) { if ( i == j) { G-> edges[ i] [ j] = 0 ; G-> edges[ j] [ i] = 0 ; } else { G-> edges[ i] [ j] = INF; G-> edges[ j] [ i] = INF; } } } }
void PrintGraph ( Graph* g)
{ for ( int i = 0 ; i < g-> n; i++ ) { printf ( "\t%c" , g-> vertex[ i] ) ; } printf ( "\n" ) ; for ( int i = 0 ; i < g-> n; i++ ) { printf ( "%c\t" , g-> vertex[ i] ) ; for ( int j = 0 ; j < g-> n; j++ ) { if ( g-> edges[ i] [ j] == INF) { printf ( "∞\t" ) ; continue ; } printf ( "%d\t" , g-> edges[ i] [ j] ) ; } printf ( "\n" ) ; }
}
void DestroyGraph ( Graph* g)
{ free ( g) ;
}
算法
# include "Graph.h"
int minKey ( int key[ ] , int mstSet[ ] , Graph* G)
{ int min = INF; int min_index = 0 ; for ( int i = 0 ; i < G-> n; i++ ) { if ( mstSet[ i] == 0 && key[ i] < min) { min = key[ i] ; min_index = i; } } return min_index;
}
void PrintMST ( int parent[ ] , Graph* G)
{ printf ( "最小生成树构成:\n" ) ; for ( int i = 1 ; i < G-> n; i++ ) { printf ( "边<%c,%c>权为:%d\n" , G-> vertex[ parent[ i] ] , G-> vertex[ i] , G-> edges[ parent[ i] ] [ i] ) ; }
}
void PrintMST2 ( int parent[ ] , Graph* G, int sequence[ ] )
{ printf ( "最小生成树构成:\n" ) ; for ( int i = 0 ; i < G-> n- 1 ; i++ ) { printf ( "边<%c,%c>权为:%d\n" , G-> vertex[ parent[ sequence[ i] ] ] , G-> vertex[ sequence[ i] ] , G-> edges[ parent[ sequence[ i] ] ] [ sequence[ i] ] ) ; }
}
void PrimMST ( Graph* G)
{ int parent[ MAX] = { 0 } ; int key[ MAX] ; int mstSet[ MAX] ; for ( int i = 0 ; i < G-> n; i++ ) { key[ i] = INF; mstSet[ i] = 0 ; } key[ 0 ] = 0 ; parent[ 0 ] = - 1 ; int sequence[ MAX] ; int j = 0 ; int n = 0 ; for ( int i = 0 ; i < G-> n- 1 ; i++ ) { int u = minKey ( key, mstSet, G) ; mstSet[ u] = 1 ; n++ ; if ( u != 0 ) { sequence[ j++ ] = u; } for ( int i = 0 ; i < G-> n; i++ ) { if ( G-> edges[ u] [ i] && mstSet[ i] == 0 && G-> edges[ u] [ i] < key[ i] ) { parent[ i] = u; key[ i] = G-> edges[ u] [ i] ; } } } int temp; int flag = 0 ; for ( int i = 1 ; i < G-> n; i++ ) { int n = 0 ; for ( int j = 0 ; j < G-> n - 1 ; j++ ) { if ( sequence[ j] == i) { break ; } else { n++ ; } if ( n == G-> n - 1 ) { temp = i; flag = 1 ; break ; } } if ( flag == 1 ) { break ; } } sequence[ j] = temp; PrintMST2 ( parent, G, sequence) ;
} int main ( int argc, char * argv[ ] )
{ Graph* G; CreateGraph ( G, 7 ) ; char V[ 8 ] = "ABCDEFG" ; for ( int i = 0 ; i < G-> n; i++ ) { G-> vertex[ i] = V[ i] ; } G-> edges[ get_pos ( G, 'A' ) ] [ get_pos ( G, 'B' ) ] = 50 ; G-> edges[ get_pos ( G, 'B' ) ] [ get_pos ( G, 'A' ) ] = 50 ; G-> edges[ get_pos ( G, 'B' ) ] [ get_pos ( G, 'D' ) ] = 65 ; G-> edges[ get_pos ( G, 'D' ) ] [ get_pos ( G, 'B' ) ] = 65 ; G-> edges[ get_pos ( G, 'A' ) ] [ get_pos ( G, 'C' ) ] = 60 ; G-> edges[ get_pos ( G, 'C' ) ] [ get_pos ( G, 'A' ) ] = 60 ; G-> edges[ get_pos ( G, 'C' ) ] [ get_pos ( G, 'D' ) ] = 52 ; G-> edges[ get_pos ( G, 'D' ) ] [ get_pos ( G, 'C' ) ] = 52 ; G-> edges[ get_pos ( G, 'D' ) ] [ get_pos ( G, 'G' ) ] = 42 ; G-> edges[ get_pos ( G, 'G' ) ] [ get_pos ( G, 'D' ) ] = 42 ; G-> edges[ get_pos ( G, 'C' ) ] [ get_pos ( G, 'G' ) ] = 45 ; G-> edges[ get_pos ( G, 'G' ) ] [ get_pos ( G, 'C' ) ] = 45 ; G-> edges[ get_pos ( G, 'B' ) ] [ get_pos ( G, 'E' ) ] = 40 ; G-> edges[ get_pos ( G, 'E' ) ] [ get_pos ( G, 'B' ) ] = 40 ; G-> edges[ get_pos ( G, 'D' ) ] [ get_pos ( G, 'E' ) ] = 50 ; G-> edges[ get_pos ( G, 'E' ) ] [ get_pos ( G, 'D' ) ] = 50 ; G-> edges[ get_pos ( G, 'D' ) ] [ get_pos ( G, 'F' ) ] = 30 ; G-> edges[ get_pos ( G, 'F' ) ] [ get_pos ( G, 'D' ) ] = 30 ; G-> edges[ get_pos ( G, 'E' ) ] [ get_pos ( G, 'F' ) ] = 70 ; G-> edges[ get_pos ( G, 'F' ) ] [ get_pos ( G, 'E' ) ] = 70 ; PrintGraph ( G) ; PrimMST ( G) ; DestroyGraph ( G) ; return 0 ;
}