A two-step algorithm for Graph adjacency table implementation

Source: Internet
Author: User

adjacency table Implementation of graphs

This structure creates adjacency tables when adding nodes more around the suggested breakpoint debugging under check

adjacency tables and adjacency matrices save space compared to adjacency tables but it's hard to tell if there are edges between the two nodes. This structure is cost-effective under sparse graphs.

Here is the code

1 #defineVertex_max 202#include"stdio.h"3#include"malloc.h"4typedefstructEdgenode5 {6     intVertex;//Vertex ordinal7     intweight;8     structEdgenode *next;//point to next vertex with Edge9}edgenode;//structure of adjacent table edgesTen  Onetypedefstruct A { -Edgenode *adjlist[vertex_max];//array of struct-body pointers for vertices -     intVextexnum,edgenum;//number of vertices number of edges the     intGraphtype;//type of diagram -      -}listgraph;//adjacency Table Structure - voidCreategraph (Listgraph *g);//Creating adjacency Tables + voidOutlist (listgraph);//Output adjacency Table -  + voidCreategraph (Listgraph *G) A { at     intI,weight; -     intStart,end; -Edgenode *s; -      for(i=1; i<=g->vextexnum;i++) -g->adjlist[i]=null;//all vertices are initialized to null -      for(i=1; i<=g->edgenum;i++) in     { - GetChar (); toprintf"Side%d:", i); +scanf"%d%d%d",&start,&end,&weight); -S= (edgenode*)malloc(sizeof(Edgenode));//request to save an edge of memory thes->next=g->adjlist[start];//point to the previous vertex location *s->vertex=end;//Save end Vertex $s->weight=weight; Panax Notoginsengg->adjlist[start]=s;//The adjacency table pair should point to that point -         if(g->graphtype==0) the         { +s= (Edgenode *)malloc(sizeof(Edgenode)); AS->next=g->Adjlist[end]; thes->vertex=start; +s->weight=weight; -g->adjlist[end]=s; $         } $     } - }  -  the voidOutlist (Listgraph *G) - {Wuyi     inti; theEdgenode *s; -      for(i=1; i<=g->vextexnum;i++) Wu     { -printf"Vertex%d", i); AboutS=g->Adjlist[i]; $          while(s) -         { -printf"->%d (%d)",s->vertex,s->weight); -S=s->Next; A         } +printf"\ n"); the     } - } $ Main () the { the listgraph G; theprintf"type of input generation diagram (0. No-map, 1. map)"); thescanf"%d",&g.graphtype); -printf"Enter the number of vertices and the number of edges of the graph"); inscanf"%d%d",&g.vextexnum,&g.edgenum); theprintf"enter the two vertices and weights that make up each edge: \ n"); theCreategraph (&G); Aboutprintf"output adjacency table \ n"); theOutlist (&G); the     return 0;  the}

A two-step algorithm for Graph adjacency table implementation

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.