图的邻接矩阵简单实现Win32版本
生活随笔
收集整理的這篇文章主要介紹了
图的邻接矩阵简单实现Win32版本
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
圖的鄰接矩陣存儲方式,結(jié)構(gòu)由頂點(diǎn)數(shù)量、邊數(shù)量、頂點(diǎn)集合和邊集合組成;
其中頂點(diǎn)集合一維數(shù)組,根據(jù)頂點(diǎn)的數(shù)量動態(tài)分配數(shù)組大小;
邊集合是二維數(shù)組,根據(jù)頂點(diǎn)的數(shù)量來動態(tài)分配數(shù)組大小,對于無向圖來說,該鄰接矩陣是對稱矩陣;
?
先做簡單;有一個無向圖和手寫其鄰接矩陣如下;
編程來實現(xiàn)一下,并輸出鄰接矩陣;頂點(diǎn)和邊都不由用戶輸入,先按給定;
代碼;
#include <windows.h> #include "resource.h"LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);HINSTANCE hInst; TCHAR szClassName[] = TEXT("graphDemo");typedef char vertexType; typedef int edgeType; typedef struct GraphMatrix{int vertexNumber; // 頂點(diǎn)數(shù)量int edgeNumber; // 邊的數(shù)量vertexType *vertex; // 頂點(diǎn)集合,動態(tài)數(shù)組edgeType** edge; // 邊集合,二維動態(tài)數(shù)組} GraphMatrix;void GraphMatrix_create(GraphMatrix *g, HDC hdc);int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil) {HWND hwnd;MSG messages;WNDCLASSEX wincl;hInst = hThisInstance;wincl.hInstance = hThisInstance;wincl.lpszClassName = szClassName;wincl.lpfnWndProc = WindowProcedure;wincl.style = CS_DBLCLKS;wincl.cbSize = sizeof (WNDCLASSEX);wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);wincl.hCursor = LoadCursor (NULL, IDC_ARROW);wincl.lpszMenuName = MAKEINTRESOURCE (IDC_GRAPHDEMO);wincl.cbClsExtra = 0;wincl.cbWndExtra = 0;wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);if (!RegisterClassEx (&wincl))return 0;hwnd = CreateWindowEx (0,szClassName,TEXT("graphDemo"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,350,350,HWND_DESKTOP,NULL,hThisInstance,NULL);ShowWindow (hwnd, nFunsterStil);while (GetMessage (&messages, NULL, 0, 0)){TranslateMessage(&messages);DispatchMessage(&messages);}return messages.wParam; }LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {PAINTSTRUCT ps;HDC hdc;RECT rt;char szBuffer[100]; GraphMatrix *gm;switch (message){case WM_COMMAND:switch (LOWORD(wParam)){case IDM_graph: hdc=GetDC(hwnd); gm=(GraphMatrix *)VirtualAlloc(NULL,sizeof(GraphMatrix),MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);GraphMatrix_create(gm,hdc);break;case IDM_ABOUT:MessageBox (hwnd, TEXT ("graphDemo v1.0\nCopyright (C) 2020\n by bo"),TEXT ("graphDemo"), MB_OK | MB_ICONINFORMATION);break;case IDM_EXIT:DestroyWindow(hwnd);break;default:return DefWindowProc(hwnd, message, wParam, lParam); }break;case WM_CREATE:break;case WM_PAINT:hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rt); EndPaint(hwnd, &ps);break;case WM_DESTROY:PostQuitMessage (0);break;default:return DefWindowProc (hwnd, message, wParam, lParam);}return 0; }void GraphMatrix_create(GraphMatrix *g, HDC hdc){int y=50; //輸出鄰接矩陣的起始Y坐標(biāo); char szBuffer[5];g->vertexNumber=5;g->edgeNumber=7;g->vertex=(vertexType*)VirtualAlloc(NULL,g->vertexNumber * sizeof(vertexType),MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);//二維動態(tài)數(shù)組申請空間g->edge=(edgeType**)VirtualAlloc(NULL,g->vertexNumber * sizeof(edgeType),MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);for (int i = 0; i < g->vertexNumber; i++){g->edge[i]=(edgeType*)VirtualAlloc(NULL,g->vertexNumber * sizeof(edgeType),MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);}//初始化鄰接矩陣的所有元素for (int i = 0; i < g->vertexNumber; i++){for (int j = 0; j < g->vertexNumber; j++)g->edge[i][j] = 0;}//輸入圖的信息g->edge[0][1] = 1; //(i,j)和(j,i)指的是一條邊g->edge[1][0] = 1;g->edge[0][3] = 1;g->edge[3][0] = 1;g->edge[0][4] = 1;g->edge[4][0] = 1;g->edge[1][2] = 1;g->edge[2][1] = 1;g->edge[1][4] = 1;g->edge[4][1] = 1;g->edge[2][3] = 1;g->edge[3][2] = 1;g->edge[3][4] = 1;g->edge[4][3] = 1; //輸出圖的信息TextOut(hdc,60,20,"圖的鄰接矩陣是:",16);for (int i = 0; i < g->vertexNumber; i++){for (int j = 0; j < g->vertexNumber; j++){wsprintf(szBuffer, "%d",g->edge[i][j]);TextOut(hdc,120+j*15,y,szBuffer,lstrlen(szBuffer));}y+=25; //下一行y坐標(biāo)增加25; } }運(yùn)行結(jié)果如下;
工程;
資源和頭文件;
#include "resource.h" #include <windows.h>/ // // Menu //IDC_GRAPHDEMO MENU BEGINPOPUP "&File"BEGINMENUITEM "圖的鄰接矩陣實現(xiàn)", IDM_graphMENUITEM "E&xit", IDM_EXITENDPOPUP "&Help"BEGINMENUITEM "&About ...", IDM_ABOUTEND END #define IDM_EXIT 10001 #define IDM_ABOUT 10002#define IDC_GRAPHDEMO 10101 #define IDD_ABOUTBOX 10102 #define IDM_graph 40001有時間再繼續(xù);
總結(jié)
以上是生活随笔為你收集整理的图的邻接矩阵简单实现Win32版本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ STL 基本使用Win32 版
- 下一篇: 超图iServer服务管理概述