圖的鄰接矩陣簡單實現Win32版本

圖的鄰接矩陣存儲方式,結構由頂點數量、邊數量、頂點集合和邊集合組成;
其中頂點集合一維數組,根據頂點的數量動態分配數組大小;
邊集合是二維數組,根據頂點的數量來動態分配數組大小,對於無向圖來說,該鄰接矩陣是對稱矩陣;

 

先做簡單;有一個無向圖和手寫其鄰接矩陣如下;

編程來實現一下,並輸出鄰接矩陣;頂點和邊都不由用戶輸入,先按給定;

代碼;

#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;            // 頂點數量
    int edgeNumber;                // 邊的數量
    vertexType *vertex;            // 頂點集合,動態數組
    edgeType** edge;            // 邊集合,二維動態數組

} 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座標; 
	char szBuffer[5];

    g->vertexNumber=5;
    g->edgeNumber=7;
    g->vertex=(vertexType*)VirtualAlloc(NULL,g->vertexNumber * sizeof(vertexType),MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);
    //二維動態數組申請空間
    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座標增加25; 
    }
}

運行結果如下;

工程;

資源和頭文件;

#include "resource.h"
#include <windows.h>

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDC_GRAPHDEMO MENU 
BEGIN
    POPUP "&File"
    BEGIN
    	MENUITEM "圖的鄰接矩陣實現",                	IDM_graph
        MENUITEM "E&xit",                	IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About ...",           	IDM_ABOUT
    END
END
#define 	IDM_EXIT		10001
#define 	IDM_ABOUT		10002

#define 	IDC_GRAPHDEMO		10101
#define 	IDD_ABOUTBOX	10102
#define     IDM_graph     40001

有時間再繼續;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章