1. How to construct a graph-----adjacency matrix

Source: Internet
Author: User

#include <iostream>using namespacestd;#defineunvistited 0//indicates that it has not been visited#defineVisited 1//indicates that it has been visited//Graph ClassclassGraph {Private:    intNumvertex, Numedge;//Number of points and number of edges    int**matrix;//adjacency Matrix    int*Mark; Public:    //constructor FunctionGraph (intNumvert)    {Init (Numvert); }    // Destructors~Graph () {Delete[]mark;  for(inti =0; i < Numvertex; i++) {            Delete[]matrix[i]; }        Delete[]matrix; }    //Initialize    voidInit (intN) {inti; Numvertex=N; Numedge=0; Mark=New int[Numvertex]; Matrix=New int*[Numvertex];  for(i =0; i < Numvertex; i++) {Mark[i]=unvistited; }         for(i =0; i < Numvertex; i++) {Matrix[i]=New int[Numvertex]; }         for(i =0; i < Numvertex; i++) {             for(intj =0; J < Numvertex; J + +) {Matrix[i][j]=0; }        }    }    //number of return points    intN () {returnNumvertex;} //returns the number of edges    intE () {returnNumedge;} //returns the first neighbor of a point v    intFirstintv) {         for(inti =0; I < n (); i++) {            if(Matrix[v][i]! =0) {                returni; }        }        return-1; }    //returns the first neighbor after W    intNextintVintW) { for(inti = w +1; I < n (); i++) {            if(Matrix[v][i]! =0) {                returni; }        }        return-1; }    //Set V1 to V2 with an edge, assign WT    voidSetedge (intV1,intV2,intwt) {        if(Matrix[v1][v2] = =0) {Matrix[v1][v2]=wt; Numedge++; }    }    //Remove the edge between V1 and v2    voidDeleteedge (intV1,intv2) {        if(Matrix[v1][v2] = =1) {Matrix[v1][v2]=0; Numedge--; }    }};intmain () {}

1. How to construct a graph-----adjacency matrix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.