導航:首頁 > 編程語言 > java鄰接圖

java鄰接圖

發布時間:2023-01-07 07:10:37

⑴ 什麼是鄰接圖

鄰接圖譜描述覆蓋了整個染色體的小片段的順序關系,這些小片段相互鄰接,
鄰接圖譜(Contig Map)
兩個片段通過有重疊部分推斷出兩者相互鄰接。

⑵ 鄰接表創建無向圖遇到的問題

如何使鄰接表的結構定義更加清晰。
(java版)用鄰接表實現無向圖的創建出現的問題是關於內部類的使用,如何使鄰接表的結構定義更加清晰,不分散。
在鄰接表中,對圖中每個頂點V建立一個單鏈表,把與V相鄰接的頂點放在這個鏈表中。

⑶ java中的單向鏈表,棧,串,有哪些對象使用的這些數據結構,還有樹,圖,廣義表這些在JAVA有哪些對象

JAVA把數據結構簡化了,提供了不少集合類(collection),用的最多的就是LIST和MAP這個兩個介面。LIST和MAP各自對應了多個實現它們的類,比如ArrayList,HashMap等等。其中List就很像C里的鏈表,它有順序存放和無序存放的對象。好像沒有幾個類能嚴格符合你說的幾種數據結構,你可以自己寫類來實現相同的功能。沒有這么多復雜的數據結構,JAVA才體現出簡單易學的特點啊。

⑷ java怎麼實現一個完全圖的鄰接矩陣的特徵值計算

如果有對稱元素 aij 和 aji 分別是1和0,那麼一定是有向圖(有一條有向邊連接兩點) 但如果所有的對應元素都相同,就無法判斷是有向圖還是無向圖

⑸ 實現圖的鄰接矩陣和圖的鄰接表的完整代碼

給你一個鄰接表的完整程序:
#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)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<endl;
}

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!=NULL&&p->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(i<n&&i>1)//先找到第i個結點,用p存第i個結點,用q存i後的結點,用h存要插入的結點
{
for(int j=1;j<i;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!=a&&p->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!=x&&p->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(i<n&&i>1)//i結點在中間的時候
{
for(int j=1;j<i;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;i<10;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;
cin>>m;
}while(m<1||m>6);//當輸入的序號不在包括中,讓他重新輸入
switch(m)
{
case 1:
{
A.Display ();
goto menu;
};break;
case 2:

{
cout<<"請輸入你想要找到的結點:"<<endl;
int c;
cin>>c;//輸入你想要找到的結點
if(A.Find (c)==1)
{
cout<<"可以找到"<<c<<endl;
A.Display ();//重新顯示出鏈表A

}
else
{
cout<<"鏈表中不存在"<<c<<endl;
A.Display ();//重新顯示出鏈表A

}

goto menu;
};break;
case 3:
{

cout<<"請選擇你要插入的方式(選擇前面的序號進行選擇)"<<endl;
cout<<"1.將特定的結點加入到特定的結點前"<<'\t'<<"2.將特定的結點加到特定的位置後"<<endl;
int b1;
do
{
cout<<"請輸入你想要插入的方式(選擇前面的序號進行選擇):"<<endl;
cin>>b1;
}while(b1<1||b1>2);//當輸入的序號不在包括中,讓他重新輸入
if(b1==1)
{
cout<<"請輸入你想要插入的數和想要插入的結點(為此結點之前插入):"<<endl;
int a1,a2;
cin>>a1>>a2;
A.Insertlist (a1,a2);//將a1插入到結點為a2結點之前
cout<<"此時鏈表為:"<<endl;
A.Display ();//重新顯示出鏈表A

}
else
{
cout<<"請輸入你想要插入的數和想要插入的位置(為此結點之後插入):"<<endl;
int a1,a2;
cin>>a1>>a2;
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;
cin>>b1;
}while(b1<1||b1>2);//當輸入的序號不在包括中,讓他重新輸入
if(b1==1)
{
cout<<"請輸入你想要刪除的結點:"<<endl;
int a;
cin>>a;//輸入你想要刪除的結點
if(A.Delete (a)==1)
{
cout<<"成功刪除"<<a<<endl;
cout<<"刪除後的鏈表為:"<<endl;
A.Display ();
}
else
{
cout<<"此鏈表為:"<<endl;
A.Display ();//重新顯示出鏈表A
cout<<"鏈表中不存在"<<a<<endl;

}

}
else
{
cout<<"請輸入你想要刪除的結點位置:"<<endl;
int b;
cin>>b;//輸入你想要刪除的結點的位置
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;i<mapsize;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!=0&&y!=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;//返回鄰接表頭
}

⑹ java中如何把圖用鄰接表表示出來

package my.graph;
import java.util.ArrayList;
import java.util.Iterator;
import my.queue.*;
import my.stack.StackX;
/**
* 鄰接表表示
* @author xiayi
*
*/
public class Graph {
private int MAX_VERTS = 20;
private Vertex vertexList[];
private boolean is = false;//是否為有向圖
private int nVerts = 0;

private StackX stackX;
private Vertex dfs[];

private Vertex bfs[];
private Queue queue;

public Graph(){
vertexList = new Vertex[MAX_VERTS];
dfs = new Vertex[MAX_VERTS];
bfs = new Vertex[MAX_VERTS];
}
public Graph(int n){
vertexList = new Vertex[n];
dfs = new Vertex[n];
bfs = new Vertex[n];

}
public Graph(int n, boolean is){
this.is = is;
vertexList = new Vertex[n];
dfs = new Vertex[n];
bfs = new Vertex[n];
}
//////////////////////////////////////////////
public boolean isIs() {
return is;
}
public void setIs(boolean is) {
this.is = is;
}
public Vertex[] getVertexList() {
return vertexList;
}
public Vertex[] getDfs() {
return dfs;
}
public Vertex[] getBfs() {
return bfs;
}

////////////////////////////////////////////////////
/**
* 添加頂點
*/
public void addVertex(Vertex vertex){
vertex.setIndex(nVerts);
vertexList[nVerts] = vertex;
nVerts++;
}
/**
* 添加邊
*/
public void addEdge(int start, int end){
vertexList[start].addAdj(vertexList[end]);
if (!is) {vertexList[end].addAdj(vertexList[start]);}
}
/**
* 返回節點個數
* @return
*/
public int getVertsCount(){
return vertexList.length;
}

/**
* 深度優先迭代器
* @return
*/
public Iterator dfsIterator(){
dfs();
return new DfsIterator();
}
/**
* 廣度優先迭代器
* @return
*/
public Iterator bfsIterator(){
bfs();
return new BfsIterator();
}
////////////////////////////////////////////////////////
public void displayGraph(){
ArrayList<Vertex> next = null;
for (int i = 0; i < vertexList.length; i++) {
printVertx(vertexList[i]);
}
}
public void printVertx(Vertex vertex){
ArrayList<Vertex> next = vertex.getAdj();
if(next == null){ System.out.println(vertex.toString()+" 無連接點");}
else{
System.out.print(vertex.toString()+"有鄰接點:");
for (int i = 0; i < next.size(); i++) {
System.out.print("頂點"+next.get(i).label+", ");
}
System.out.println();
}
}

///////////////////////////////////////////////////////////

public void dfs(){
stackX = new StackX(MAX_VERTS);
vertexList[0].isVisted = true;
dfs[0] = vertexList[0];

stackX.push(vertexList[0]);
int dfsIndex = 0;

Vertex vertex;
while(!stackX.isEmpty()){
vertex = getAdjVertex((Vertex)stackX.peek());
if(vertex == null){
stackX.pop();
}else{
vertex.isVisted = true;
dfs[++dfsIndex]=vertex;
stackX.push(vertex);
}
}

for (int i = 0; i < getVertsCount(); i++) {
vertexList[i].isVisted = false;
}

}

public void bfs() {
queue = new Queue(MAX_VERTS);
vertexList[0].isVisted = true;
bfs[0] = vertexList[0];
queue.insert(vertexList[0]);
int bfsIndex = 0;
Vertex vertex;
while(!queue.isEmpty()){
Vertex vertex2 = (Vertex)queue.remove();
while((vertex = getAdjVertex(vertex2))!=null){
vertex.isVisted = true;
bfs[++bfsIndex] = vertex;
queue.insert(vertex);
}
}

for (int i = 0; i < getVertsCount(); i++) {
vertexList[i].isVisted = false;
}
}
/**
* 得到一個鄰接點
* @param vertex
* @return
*/
public Vertex getAdjVertex(Vertex vertex){
ArrayList<Vertex> adjVertexs = vertex.getAdj();
for (int i = 0; i < adjVertexs.size(); i++) {
if(!adjVertexs.get(i).isVisted){
return adjVertexs.get(i);
}
}
return null;
}
/////////////////////////////////////////////////////////////
private abstract class GraphIterator implements Iterator{

int count = 0;
public GraphIterator(){
}
public boolean hasNext() {
return count != getVertsCount()-1;
}
public Object next() {
// TODO Auto-generated method stub
return null;
}
public void remove() {
// TODO Auto-generated method stub

}

}
//深度優先迭代
private class DfsIterator extends GraphIterator{
public DfsIterator(){
super();
}

public Vertex next() {
return dfs[count++];
}
}
//廣度優先迭代
private class BfsIterator extends GraphIterator{
public BfsIterator(){
super();
}

public Object next() {
return bfs[count++];
}
}
/////////////////////////////////////////////////////////

public static void main(String[] args) {
int nVerts = 10;
int c = 'A'-1;
Vertex vertex;
Graph myGraph = new Graph(nVerts, false);
for (int i = 0; i < nVerts; i++) {
c++;
vertex = new Vertex((char)(c));
myGraph.addVertex(vertex);
}
myGraph.addEdge(0, 1);
myGraph.addEdge(0, 4);
myGraph.addEdge(1, 2);
myGraph.addEdge(2, 3);
myGraph.addEdge(4, 5);
myGraph.addEdge(4, 6);
myGraph.addEdge(5, 8);
myGraph.addEdge(6, 7);
myGraph.addEdge(7, 8);
myGraph.addEdge(8, 9);

System.out.println("深度優先迭代遍歷:");
for (Iterator iterator = myGraph.dfsIterator(); iterator.hasNext();) {
vertex = (Vertex) iterator.next();
System.out.println(vertex.toString());

}

System.out.println("/n廣度優先迭代遍歷:");
for (Iterator iterator = myGraph.bfsIterator(); iterator.hasNext();) {
vertex = (Vertex) iterator.next();
System.out.println(vertex.toString());

}
}
}
class Vertex{
public char label;
public boolean isVisted;
public int index;
private ArrayList<Vertex> next = null;
public Vertex(char lab) // constructor
{
label = lab;
isVisted = false;
}
//為節點添加鄰接點
public void addAdj(Vertex ver){
if(next == null) next = new ArrayList<Vertex>();
next.add(ver);
}

public ArrayList<Vertex> getAdj(){
return next;
}

public void setIndex(int index){
this.index = index;
}

public String toString(){
return "頂點 "+label+",下標:"+index+".";
}
}

代碼來自:http://blog.csdn.net/Java2King/article/details/5683429

閱讀全文

與java鄰接圖相關的資料

熱點內容
公司法pdf下載 瀏覽:379
linuxmarkdown 瀏覽:347
華為手機怎麼多選文件夾 瀏覽:679
如何取消命令方塊指令 瀏覽:345
風翼app為什麼進不去了 瀏覽:774
im4java壓縮圖片 瀏覽:358
數據查詢網站源碼 瀏覽:146
伊克塞爾文檔怎麼進行加密 瀏覽:886
app轉賬是什麼 瀏覽:159
php的基本語法 瀏覽:792
對外漢語pdf 瀏覽:516
如何用mamp本地web伺服器 瀏覽:869
如何加密自己js代碼 瀏覽:627
排列組合a與c的演算法 瀏覽:534
如何在文件夾中找到同名內容 瀏覽:786
有什麼app文字轉韓文配音 瀏覽:372
循環宏1命令 瀏覽:35
斐波那契數列矩陣演算法 瀏覽:674
公式保護後加密不了 瀏覽:82
java跳轉到jsp 瀏覽:819