第十三周專案1Prim 演算法驗證

2021-07-07 09:36:55 字數 2195 閱讀 5614

問題及描述:

(1)prim演算法

graph.h:

#ifndef graph_h_included

#define graph_h_included

#define maxv 100 //最大頂點個數

#define inf 32767 //inf表示∞

typedef int infotype;

//以下定義鄰接矩陣型別

typedef struct

vertextype; //頂點型別

typedef struct //圖的定義

mgraph; //圖的鄰接矩陣型別

//以下定義鄰接表型別

typedef struct anode //弧的結點結構型別

arcnode;

typedef int vertex;

typedef struct vnode //鄰接表頭結點的型別

vnode;

typedef vnode adjlist[maxv]; //adjlist是鄰接表型別

typedef struct

algraph; //圖的鄰接表型別

//功能:由乙個反映圖中頂點鄰接關係的二維陣列,構造出用鄰接矩陣儲存的圖

//引數:arr - 陣列名,由於形式引數為二維陣列時必須給出每行的元素個數,在此將引數arr宣告為一維陣列名(指向int的指標)

// n - 矩陣的階數

// g - 要構造出來的鄰接矩陣資料結構

void arraytomat(int *arr, int n, mgraph &g); //用普通陣列構造圖的鄰接矩陣

void arraytolist(int *arr, int n, algraph *&); //用普通陣列構造圖的鄰接表

void mattolist(mgraph g,algraph *&g);//將鄰接矩陣g轉換成鄰接表g

void listtomat(algraph *g,mgraph &g);//將鄰接表g轉換成鄰接矩陣g

void dispmat(mgraph g);//輸出鄰接矩陣g

void dispadj(algraph *g);//輸出鄰接表g

#endif // graph_h_included

graph.cpp

#include #include #include "graph.h"

//功能:由乙個反映圖中頂點鄰接關係的二維陣列,構造出用鄰接矩陣儲存的圖

//引數:arr - 陣列名,由於形式引數為二維陣列時必須給出每行的元素個數,在此將引數arr宣告為一維陣列名(指向int的指標)

// n - 矩陣的階數

// g - 要構造出來的鄰接矩陣資料結構

void arraytomat(int *arr, int n, mgraph &g)

g->e=count;

}void mattolist(mgraph g, algraph *&g)

//將鄰接矩陣g轉換成鄰接表g

g->n=g.n;

g->e=g.e;

}void listtomat(algraph *g,mgraph &g)

//將鄰接表g轉換成鄰接矩陣g

}}void dispmat(mgraph g)

//輸出鄰接矩陣g

printf("\n");}}

main.cpp

#include #include #include "graph.h"

void prim(mgraph g,int v)

{ int lowcost[maxv]; //頂點i是否在u中

int min;

int closest[maxv],i,j,k;

for (i=0; i

執行結果:

學習心得:跟著**走一遍

第十三周專案1 prim演算法

檔名稱 專案1.cbp 作 者 孫欽達 完成日期 2015年12月4日 版 本 號 v1.0 問題描述 prim演算法 輸入描述 無 程式輸出 最小生成樹 ifndef graph h included define graph h included define maxv 100 最大頂點個數 d...

第十三周專案1 prim演算法

檔名稱 專案5.cbp 作 者 臧曉彤 完成日期 2015年12月18日 版 本 號 v1.0 問題描述 prim演算法 輸入描述 無 程式輸出 最小生成樹 ifndef graph h included define graph h included define maxv 100 最大頂點個數 ...

第十三周專案1 prim演算法的驗證

檔名稱 prim.cbp 作 者 李涵睿 完成日期 2015年11月23日 版 本 號 v1.0 問題描述 prim演算法的驗證。輸入描述 無 程式輸出 測試資料 標頭檔案和原始檔見 圖的演算法庫 include include include include graph.h using names...