當前位置:首頁 » 存儲配置 » e以鄰接表存儲

e以鄰接表存儲

發布時間: 2022-07-17 14:32:26

⑴ 為什麼當以鄰接表作存儲結構時,深度優先搜索遍歷圖的時間復雜度為O(n+e)

n是因為要對每一個節點都做dfs,e是因為dfs只要把所有的邊都走到了,就跳出了.

⑵ 如題,以鄰接表存儲圖,並對圖進行深度優先遍歷

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAXVEX 100
typedef char VertexType[3];/*定義VertexType為char數組類型*/
typedef struct vertex
{int adjvex; /*頂點編號*/VertexType data; /*頂點的信息*/ } VType;/*頂點類型*/
typedef struct graph
{int n,e;/*n為實際頂點數,e為實際邊數*/
VType vexs[MAXVEX];/*頂點集合*/
int edges[MAXVEX][MAXVEX];/*邊的集合*/
} AdjMatix;/*圖的鄰接矩陣類型*/
typedef struct edgenode
{int adjvex; /*鄰接點序號*/ int value;/*邊的權值*/struct edgenode *next;/*下一頂點*/
} ArcNode;/*每個頂點建立的單鏈表中結點的類型*/
typedef struct vexnode
{VertexType data; /*結點信息*/ArcNode *firstarc;/*指向第一條邊結點*/
} VHeadNode;/*單鏈表的頭結點類型*/
typedef struct
{int n,e;/*n為實際頂點數,e為實際邊數*/
VHeadNode adjlist[MAXVEX];/*單鏈表頭結點數組*/
} AdjList; /*圖的鄰接表類型*/
void DispAdjList(AdjList *G)
{int i;
ArcNode *p;
printf("圖的鄰接表表示如下:\n");
for (i=0;i<G->n;i++)
{printf(" [%d,%3s]=>",i,G->adjlist[i].data);
p=G->adjlist[i].firstarc;
while (p!=NULL)
{printf("(%d,%d)->",p->adjvex,p->value);p=p->next;}printf("∧\n");
}
}
void MatToList(AdjMatix g,AdjList *&G) /*將鄰接矩陣g轉換成鄰接表G*/
{int i,j;ArcNode *p;
G=(AdjList *)malloc(sizeof(AdjList));
for (i=0;i<g.n;i++)/*給鄰接表中所有頭結點的指針域置初值*/
{G->adjlist[i].firstarc=NULL;strcpy(G->adjlist[i].data,g.vexs[i].data);}
for (i=0;i<g.n;i++)/*檢查鄰接矩陣中每個元素*/
for (j=g.n-1;j>=0;j--)
if (g.edges[i][j]!=0)/*鄰接矩陣的當前元素不為0*/
{p=(ArcNode *)malloc(sizeof(ArcNode));/*創建一個結點*p*/
p->value=g.edges[i][j];p->adjvex=j;
p->next=G->adjlist[i].firstarc; G->adjlist[i].firstarc=p;}
G->n=g.n;G->e=g.e;
}
int visited[MAXVEX];
void DFS(AdjList *g,int vi)/*對鄰接表G從頂點vi開始進行深度優先遍歷*/
{ArcNode *p;printf("%d ",vi);/*訪問vi頂點*/ visited[vi]=1;/*置已訪問標識*/
p=g->adjlist[vi].firstarc;/*找vi的第一個鄰接點*/
while (p!=NULL)/*找vi的所有鄰接點*/
{if (visited[p->adjvex]==0)DFS(g,p->adjvex);p=p->next; }
}
void main()
{int i,j,k,a[9][9];AdjMatix g;AdjList *G;
char *vname[MAXVEX]={"a","b","c","d","e","f","g","h","i"};
printf("輸入定點數(<10),邊數");
scanf("%d,%d",&i,&k);
g.n=i;g.e=2*k;
for (i=0;i<g.n;i++)strcpy(g.vexs[i].data,vname[i]);
for (i=0;i<g.n;i++)
for (j=0;j<g.e;j++)g.edges[i][j]=0; /*a[i][j];*/
for(k=0;k<g.e/2;k++)
{printf("請輸入第%d條邊的起點和終點序號(逗點分隔)",k);scanf("%d,%d",&i,&j);
g.edges[i][j]=g.edges[j][i]=1;}
MatToList(g,G);/*生成鄰接表*/ DispAdjList(G);/*輸出鄰接表*/
for (i=0;i<g.n;i++)visited[i]=0; /*頂點標識置初值*/
printf("從頂點0的深度優先遍歷序列:\n");printf(" 遞歸演算法:");DFS(G,0);printf("\n");
}

⑶ 設G=(V,E)以鄰接表儲存,如圖所示,試畫出從頂點1出發所得到的深度優先生成樹

深度優先生成樹
1-2-3-4-5
廣度優先生成樹
1
/|\
/ | \
2 3 4
|
5

⑷ 實驗題目: 以鄰接表儲存圖並對圖進行遍歷(寫出流程圖及程序) 大神們 幫幫忙吧!!!

#include "stdlib.h"
#include "stdio.h"
#define N 6
#define e 4
typedef int vextype;
typedef struct node
{
int adjvex; /*鄰接點域*/
struct node *next; /*鏈域*/
}edgenode; /*邊表結點*/

typedef struct
{
vextype vertex; /*頂點信息*/
edgenode *link; /*邊表頭指針*/
}vexnode; /*頂點表結點*/

void CREATADJLIST(vexnode *ga)
{
int i,j,k;
edgenode *s;
for (i=0;i<N;i++) //輸入頂點信息;
{
ga[i].vertex=i+1;
ga[i].link=NULL; //邊表頭指針初始化;
}
printf("please enter the (i,j):");
for (k=0;k<e;k++) //建立邊表;
{
scanf("%d%d",&i,&j); //讀入邊(VI,VJ)的頂點對序號;
s=(edgenode*)malloc(sizeof(edgenode)); //生成鄰接點序號為J的表結點S;
s->adjvex=j;
s->next=ga[i-1].link;
ga[i-1].link=s; //將S插入頂點VI的邊表頭部;

s=(edgenode*)malloc(sizeof(edgenode)); ////生成鄰接點序號為I的表結點S;
s->adjvex=i;
s->next=ga[j-1].link;
ga[j-1].link=s; //將S插入頂點VJ的邊表頭部;
}
}
bool visited[N];
void DFSL(int i,vexnode *ga) //從VI出發深度優先搜索圖ga,;
{
edgenode *p;
printf("node:%d\n",ga[i-1].vertex);
visited[i-1]=true;
p=ga[i-1].link;
while (p!=NULL)
{
if (visited[p->adjvex-1]!=true)
{
DFSL(p->adjvex,ga);
}
p=p->next;
}
}
int head;
int tail;
int body[100];
void makeq()
{
head = 0;
tail = 0;
}
void inq(int a)
{
body[tail] = a;
tail++;
}
int outq()
{
int a;
a = body[head];
head++;
return a;
}
void BFSL(int i,vexnode *ga)
{
makeq();
int a;
for (a = 0; a < N; a++)
{
visited[a] = false;
}
edgenode *p;
printf("node:%d\n",ga[i-1].vertex);
visited[i-1]=true;
inq(i);
while (head != tail)
{
a = outq();
p=ga[a-1].link;
while (p!=NULL)
{
if (visited[p->adjvex-1]!=true)
{
printf("node:%d\n",ga[p->adjvex-1].vertex);
visited[p->adjvex-1] = true;
inq(p->adjvex);
}
p=p->next;
}
}
}

int main()
{
vexnode ga[N];
CREATADJLIST(ga);
int i;
printf("please enter the node which is start(1-6):");
scanf("%d",&i);
printf("DFSL:\n");
DFSL(i,ga);
printf("BFSL:\n");
BFSL(i,ga);
return 0;
}

⑸ 如何用鄰接表存儲圖結構

我看不太懂這個程序,不過我有些過圖的鄰接表表示,看對你有沒有幫助吧。
#include <iostream>
#include <fstream>
#include <vector>

typedef int QElemTyep;
#include "queue.h"
using namespace std;
typedef int Status;
#define MAX_VERTEX_NUM 30 //圖的最大頂點數
enum BOOL {False,True};
BOOL visited[MAX_VERTEX_NUM]; //全局變數--訪問標志數組

typedef struct ArcNode{
//弧結點
int adjvex; //該弧所指向的頂點的位置
struct ArcNode *nextarc; //指向下一條弧的指針
InfoType *info; //保存邊的信息,可以簡單的改為 int w;
}ArcNode;
typedef struct VNode{
VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];

class Graph{
public: AdjList vertices; //記錄頂點信息,指向第一條依附該頂點的弧的指針
int vexnum,arcnum; //圖的當前頂點和弧數
int GraphKind; //圖的種類,0---無向圖,1---有向圖
Graph(int vexnum,int arcnum,int kind)
{
this->vexnum=vexnum;
this->arcnum=arcnum;
this->GraphKind=kind;
}
};
void CreateGraph(Graph &G,VertexType *V,ArcType *VR){
//構造鄰接表結構的圖G

int i;
ArcNode *s;
for(i=1;i<=G.vexnum;i++) //初始化指針數組
{
G.vertices[i].data=V[i];
G.vertices[i].firstarc=NULL;
}
for(i=1;i<=G.arcnum;i++)
{
s=(ArcNode *)malloc(sizeof(ArcNode)); //生成一個弧結點
s->nextarc=G.vertices[VR[i].start].firstarc; //插入到鄰接表中
s->adjvex=VR[i].end;
G.vertices[VR[i].start].firstarc=s;

if(G.GraphKind==0) {
//若是無向圖,再插入到終點的弧鏈中
s=(ArcNode *)malloc(sizeof(ArcNode));
s->nextarc=G.vertices[VR[i].end].firstarc;
s->adjvex=VR[i].start;
G.vertices[VR[i].end].firstarc=s;
}
}
}

⑹ 數據結構 設G=(V,E)以鄰接表存儲,試寫出深度優先和廣度優先序列.

深度優先序列:1-2-3-4-5
廣度優先序列:1-2-3-4-5

⑺ 鄰接表存儲時,空間復雜度O( n+e),還是O(n)

O(n+e),取n次最小權,每次取完會進行n次更新。如果能達到o(n+e),就不需要O(n)。

在有向圖中,描述每個點向別的節點連的邊(點a->點b這種情況)。在無向圖中,描述每個點所有的邊。與鄰接表相對應的存圖方式叫做邊集表,這種方法用一個容器存儲所有的邊。

對於有向圖,vi的鄰接表中每個表結點都對應於以vi為始點射出的一條邊。因此,將有向圖的鄰接表稱為出邊表。



(7)e以鄰接表存儲擴展閱讀:

n個頂點e條邊的有向圖,它的鄰接表表示中有n個頂點表結點和e個邊表結點。(因為有向圖是單向的)

在有向圖中,為圖中每個頂點vi建立一個入邊表的方法稱逆鄰接表表示法。入邊表中的每個表結點均對應一條以vi為終點(即射入vi)的邊。

n個頂點e條邊的有向圖,它的逆鄰接表表示中有n個頂點表結點和e個邊表結點。

⑻ 採用鄰接表存儲,拓撲排序演算法的時間復雜度為多少

要看使用什麼樣的拓撲排序,最好的方法是輸出DFS的逆序,這樣的演算法復雜度是O(V+L),V是頂點個數,L是邊個數。

熱點內容
創建實例在linux 發布:2024-10-07 18:03:16 瀏覽:485
黑客學c語言 發布:2024-10-07 17:37:39 瀏覽:942
ftp比較文件 發布:2024-10-07 17:04:56 瀏覽:39
如何配置幼兒園園內的玩具 發布:2024-10-07 17:04:23 瀏覽:863
干支日演算法 發布:2024-10-07 16:47:17 瀏覽:502
sqlin語句用法 發布:2024-10-07 16:45:05 瀏覽:640
直出伺服器怎麼樣 發布:2024-10-07 15:41:36 瀏覽:479
比亞迪唐dmi哪個配置性價比 發布:2024-10-07 15:19:28 瀏覽:903
編譯器按變數 發布:2024-10-07 15:07:03 瀏覽:775
怎麼忘記電腦wifi密碼怎麼辦 發布:2024-10-07 15:02:18 瀏覽:426