本文目錄一覽:
- 1、用python實現鄰接矩陣轉換為鄰接表,python語言實現
- 2、請編寫一個完整的程序,建立有向圖的鄰接表存儲結構,要求:
- 3、鄰接表 判斷有向圖是否有環 python
- 4、如何用鄰接表存儲圖結構
用python實現鄰接矩陣轉換為鄰接表,python語言實現
graph = {‘A’: [‘B’, ‘C’],
‘B’: [‘C’, ‘D’],
‘C’: [‘D’],
‘D’: [‘C’,’G’,’H’],
‘E’: [‘F’],
‘F’: [‘C’]}
#從圖中找出任意一條從起始頂點到終止頂點的路徑
def find_path(graph, start, end, path=[]):
if start == end:
print “path”, path
return True
if not graph.get(start):
path.pop()
return False
for v in graph:
if v not in path:
path.append(v)
if find_path(graph,v,end,path):
return True
return False
path = []
if find_path(graph, ‘A’, ‘C’, path=path):
print(path)
else:
print(1)
#從圖中找出從起始頂點到終止頂點的所有路徑
import copy
def find_path_all(curr, end, path):
”’
:param curr: 當前頂點
:param end: 要到達的頂點
:param path: 當前頂點的一條父路徑
:return:
”’
if curr == end:
path_tmp = copy.deepcopy(path)
path_all.append(path_tmp)
return
if not graph.get(curr):
return
for v in graph[curr]:
#一個頂點在當前遞歸路徑中只能出現一次,否則會陷入死循環。
if v in path:
print(“v %s in path %s” %(v, path))
continue
#構造下次遞歸的父路徑
path.append(v)
find_path_all(v,end,path)
path.pop()
path_all = []
find_path_all(‘A’, ‘G’,path=[‘A’])
print path_all
#遍歷圖中所有頂點,按照遍歷順序將頂點添加到列表中
vertex = []
def dfs(v):
if v not in graph:
return
for vv in graph[v]:
if vv not in vertex:
vertex.append(vv)
dfs(vv)
for v in graph:
if v not in vertex:
vertex.append(v)
dfs(v)
print(vertex)
請編寫一個完整的程序,建立有向圖的鄰接表存儲結構,要求:
給你一個鄰接表的完整程序:
#include iostream.h
struct node
{
int data;
node *next;
};
class list
{
public:
list(){head=NULL;};
void MakeEmpty();
int Length();
void Insert(int x,int i);//將x插入到第i個結點(不含頭結點)的之後
void Insertlist(int a,int b);//將節點b插入a之前
int Delete(int x);
int Remove(int i);
int Find(int x);
void Display();
private:
node *head;
};
void list::Display()
{
node *current=head;
while (current!=NULL)
{
coutcurrent-data” “;
current=current-next;
}
coutendl;
}
void list::MakeEmpty()
{
head=NULL;
}
int list::Length()
{int n=1;
node *q=head;
if(q==NULL)
n=1;
else
while(q!=NULL)
{
n++;
q=q-next;
}
return n;
}
int list::Find(int x)//在鏈表中查找數值為x的結點,成功返回1,否則返回0
{
node *p=head;
while(p!=NULLp-data!=x)
p=p-next;
if(p-data==x)
return 1;
else
return 0;
}
void list::Insert (int x,int i)//將x插入到第i個結點(不含頭結點)的之後;
{
node *p;//p中放第i個結點
node *q;//q中放i後的結點
node *h;//h中存要插入的結點
h=new node;
h-data =x;
p=head;
if(p-next !=NULL) //鏈表不是只有一個結點或者空鏈表時候
{
int n=1;
while(p-next !=NULL)
{
n++;
p=p-next ;
}// 得到鏈表的結點的個數
p=head;//使p重新等於鏈首
if(i==n)//i=n時,直接加在最後面就行了
{
while(p-next !=NULL)
p=p-next;
p-next=h;
h-next =NULL;
}
else if(ini1)//先找到第i個結點,用p存第i個結點,用q存i後的結點,用h存要插入的結點
{
for(int j=1;ji;j++)
p=p-next;//找到第i個結點,用p存第i個結點
q=p-next;//q存i後的結點
p-next=h;
h-next=q;
}
else
cout”超出鏈表結點個數的範圍”endl;
}
else
cout”這個鏈表是空鏈表或者結點位置在首位”endl;
}
void list::Insertlist(int a,int b)//將b插入到結點為a之前
{
node *p,*q,*s;//p所指向的結點為a,s所指為要插入的數b,q所指向的是a前的結點
s=new node;
s-data=b;
p=head;
if(head==NULL)//空鏈表的時候
{
head=s;
s-next=NULL;
}
else
if(p-data==a)//a在鏈首時候
{
s-next=p;
head=s;
}
else
{
while(p-data!=ap-next!=NULL)//使p指向結點a,q指向a之前的結點
{
q=p;
p=p-next;
}
if(p-data==a)//若有結點a時候
{
q-next=s;
s-next=p;
}
else//沒有a的時候
{
p-next=s;
s-next=NULL;
}
}
}
int list::Delete(int x)//刪除鏈表中值為x的結點,成功返回1,否則返回0;
{
node *p,*q;
p=head;
if(p==NULL)
return 0;
if(p-data==x)
{
head=p-next;
delete p;
return 1;
}
else
{
while(p-data!=xp-next!=NULL)
{ q=p;
p=p-next;
}
if(p-data==x)
{
q-next =p-next;
delete p;
return 1;
}
else
return 0;
}
}
int list::Remove(int i)
{
node *p,*q;
p=head;
if(p!=NULL)
{ int n=1;
while(p-next !=NULL)
{
n++;
p=p-next ;
}//得到鏈表結點的個數
p=head;
if(i==n)//i結點在結尾的時候
{
while(p-next!=NULL)
{
q=p;
p=p-next;
}
q-next=NULL;
delete p;
return 1;
}
else if(ini1)//i結點在中間的時候
{
for(int j=1;ji;j++)
{
q=p;//q中放i前的結點
p=p-next ;//p中放第i個結點
}
q-next=p-next;
delete p;
return 1;
}
else if(i==1)//i結點在首位的時候
{
q=p-next;
head=q;
delete p;
return 1;
}
else
return 0;
}
else
return 0;
}
void main()
{
list A;
int data[10]={1,2,3,4,5,6,7,8,9,10};
A.Insertlist(0,data[0]);
for(int i=1;i10;i++)
A.Insertlist(0,data[i]);
A.Display();
menu:cout”1.遍歷鏈表”‘\t'”2.查找鏈表”‘\t'”3.插入鏈表”endl;
cout”4.刪除鏈表”‘\t'”5.鏈表長度”‘\t'”6.置空鏈表”endl;
int m;
do
{
cout”請輸入你想要進行的操作(選擇對應操作前面的序號):”endl;
cinm;
}while(m1||m6);//當輸入的序號不在包括中,讓他重新輸入
switch(m)
{
case 1:
{
A.Display ();
goto menu;
};break;
case 2:
{
cout”請輸入你想要找到的結點:”endl;
int c;
cinc;//輸入你想要找到的結點
if(A.Find (c)==1)
{
cout”可以找到”cendl;
A.Display ();//重新顯示出鏈表A
}
else
{
cout”鏈表中不存在”cendl;
A.Display ();//重新顯示出鏈表A
}
goto menu;
};break;
case 3:
{
cout”請選擇你要插入的方式(選擇前面的序號進行選擇)”endl;
cout”1.將特定的結點加入到特定的結點前”‘\t'”2.將特定的結點加到特定的位置後”endl;
int b1;
do
{
cout”請輸入你想要插入的方式(選擇前面的序號進行選擇):”endl;
cinb1;
}while(b11||b12);//當輸入的序號不在包括中,讓他重新輸入
if(b1==1)
{
cout”請輸入你想要插入的數和想要插入的結點(為此結點之前插入):”endl;
int a1,a2;
cina1a2;
A.Insertlist (a1,a2);//將a1插入到結點為a2結點之前
cout”此時鏈表為:”endl;
A.Display ();//重新顯示出鏈表A
}
else
{
cout”請輸入你想要插入的數和想要插入的位置(為此結點之後插入):”endl;
int a1,a2;
cina1a2;
A.Insert (a1,a2);//將a1插入到結點位置為a2的結點之後
cout”此時鏈表為:”endl;
A.Display ();//重新顯示出鏈表A
}
goto menu;
};break;
case 4:
{
cout”請選擇你要刪除的方式(選擇前面的序號進行選擇)”endl;
cout”1.刪除特定的結點”‘\t'”2.刪除特定位置的結點”endl;
int b1;
do
{
cout”請輸入你想要插入的方式(選擇前面的序號進行選擇):”endl;
cinb1;
}while(b11||b12);//當輸入的序號不在包括中,讓他重新輸入
if(b1==1)
{
cout”請輸入你想要刪除的結點:”endl;
int a;
cina;//輸入你想要刪除的結點
if(A.Delete (a)==1)
{
cout”成功刪除”aendl;
cout”刪除後的鏈表為:”endl;
A.Display ();
}
else
{
cout”此鏈表為:”endl;
A.Display ();//重新顯示出鏈表A
cout”鏈表中不存在”aendl;
}
}
else
{
cout”請輸入你想要刪除的結點位置:”endl;
int b;
cinb;//輸入你想要刪除的結點的位置
if(A.Remove(b)==1)
{
cout”成功刪除第”b”個結點”endl;
cout”刪除後的鏈表為:”endl;
A.Display ();//重新顯示出鏈表A
}
else
{
cout”當前鏈表的結點個數為:”A.Length ()endl;
cout”您輸入的結點位置越界”endl;
}
}
goto menu;
};break;
case 5:
{
cout”這個鏈表的結點數為:”A.Length ()endl;
goto menu;
};break;
case 6:
{
A.MakeEmpty ();
cout”這個鏈表已經被置空”endl;
goto menu;
};break;
}
}
評論(3)|1
sunnyfulin |六級採納率46%
擅長:C/C++JAVA相關Windows數據結構及算法百度其它產品
按默認排序|按時間排序
其他1條回答
2012-04-23 17:41121446881|六級
我寫了一個C語言的,只給你兩個結構體和一個初始化函數:
#include “stdio.h”
#include “malloc.h”
struct adjacentnext//鄰接表項結構體
{
int element;
int quanvalue;
struct adjacentnext *next;
};
struct adjacenthead//鄰接表頭結構體
{
char flag;
int curvalue;
int element;
struct adjacenthead *previous;
struct adjacentnext *son;
};
//初始化圖,用鄰接表實現
struct adjacenthead *mapinitialnize(int mapsize)
{
struct adjacenthead *ahlists=NULL;
struct adjacentnext *newnode=NULL;
int i;
int x,y,z;
ahlists=malloc(sizeof(struct adjacenthead)*mapsize);
if(ahlists==NULL)
return NULL;
for(i=0;imapsize;i++)
{
ahlists[i].curvalue=0;
ahlists[i].flag=0;
ahlists[i].previous=NULL;
ahlists[i].son=NULL;
ahlists[i].element=i+1;
}
scanf(“%d%d%d”,x,y,z);//輸入源結點,目的結點,以及源結點到目的結點的路權值
while(x!=0y!=0)//x,y至少有一個零就結束
{
newnode=malloc(sizeof(struct adjacentnext));
newnode-element=y;
newnode-quanvalue=z;
newnode-next=ahlists[x-1].son;
ahlists[x-1].son=newnode;
scanf(“%d%d%d”,x,y,z);
}
return ahlists;//返回鄰接表頭
}
鄰接表 判斷有向圖是否有環 python
鄰接表還是逆鄰接表看如果是逆鄰接表,每個頂點出發鄰接表的鏈表中的結點個數就是入度
如果是鄰接表過程如下:
有一個輔助數組,大小就是頂點數量,所有元素初值都為0
從頭到尾遍歷每個頂點出發的鄰接表的結點,只要當前結點的數據是幾(也就是第幾個結點被有向弧進入了),這個下標的輔助數組元素加1,等所有的鄰接表的小鏈表遍歷完了,這個輔助數組中各個下標的數字就是該頂點的入度
如何用鄰接表存儲圖結構
我看不太懂這個程序,不過我有些過圖的鄰接表表示,看對你有沒有幫助吧。
#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;
}
}
}
原創文章,作者:YXAPN,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/128054.html