当前位置:首页 > 代码 > 正文

拓扑控制程序代码(拓扑控制算法)

admin 发布:2022-12-19 09:39 106


今天给各位分享拓扑控制程序代码的知识,其中也会对拓扑控制算法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求C#网络拓扑结构图程序的构思与代码

/**

* Calculator

* A shareware calculator

*

* @author {@link Bingbing Li}

*

* @recitation 0101 Matt Carlson

*

* @date 12/7/2005 12:08AM

*

*/

import javax.swing.*;

import javax.swing.border.*;

import java.awt.*;

import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener

{

public static final int WIDTH = 800; // width of the window

public static final int HEIGHT = 600; // height of the window

public static final int BUTTON_WIDTH = 80; // width of the buttons

public static final int BUTTON_HEIGHT = 60; // height of the buttons

private CardLayout dealer; // card layout

private JPanel deckPanel; // used to card layout

private JTextField result; // the calculate result

private JCheckBoxMenuItem scientificMode; // the menu item of the mode

private Box vPanel1; // the first line buttons of the

// scientific mode.

private JButton mod; // the modular button

private JButton xey; // the button of Yth root of X

private JButton ms; // save button

private JButton mr; // release the stored value

private JButton mc; // clear the stored value

private double head = 0.0; // the first number of the equation

private double tail = 0.0; // the second number of the equation

private boolean substitution = true; // whether substituted the answer or not

private String operatedCommand = "NOTHING"; // the current operated command

private String preOperatedCommand = "NOTHING"; // remember the pre-operated command

private double variableMemory = 0.0; // the variable of the memory

private JButton jButtonR; // the register's button

private JButton jButtonE; // the edit's button

private JTextField nameField; // the field of the name

private JTextField countryField; // the field of the country

private JTextField zipField; // the field of the zip

private JTextField stateField; // the field of the state

private JTextField cityField ; // the field of the city

private JTextField streetAddressField; // the field of the address

private JTextField first; // the first part of the key

private JTextField second; // the second part of the key

private JTextField third; // the third part of the key

private JTextField fourth; // the fourth part of the key

public Calculator()

{

setSize(WIDTH, HEIGHT);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setTitle("Normal Calculator");

Container contentPre = getContentPane(); // the container of the window

dealer = new CardLayout(); // create a new card layout

deckPanel = new JPanel();

deckPanel.setLayout(dealer);

Box content = Box.createVerticalBox(); // create a new vertical Box

Box registration = Box.createVerticalBox();

/****************************************************

* menu

* file

*****************************************************/

JMenu fileMenu = new JMenu("File");

JMenuItem exitItemOfFile = new JMenuItem("Exit");

exitItemOfFile.addActionListener(this);

fileMenu.add(exitItemOfFile);

/**

* menu

* settings

*/

JMenu settingsMenu = new JMenu("Settings");

JMenuItem registrationInfo = new JMenuItem("Registration Info");

registrationInfo.addActionListener(this);

settingsMenu.add(registrationInfo);

scientificMode = new JCheckBoxMenuItem("Scientific Mode");

scientificMode.addActionListener(this);

settingsMenu.add(scientificMode);

JMenuBar jMenuBar = new JMenuBar();

jMenuBar.add(fileMenu);

jMenuBar.add(settingsMenu);

setJMenuBar(jMenuBar);

/****************************************************

* textFiled panel

*****************************************************/

result = new JTextField("0"); // the initiated value of the answer

result.setBackground(Color.CYAN); // set the back ground color with cyan

result.setHorizontalAlignment(JTextField.RIGHT); // set the horizontal alignment

content.add(result);

content.add(paintButtons());

deckPanel.add("cal", content); // add the calculators card to the layout

registration.add(paintRegistration()); // add the register window

deckPanel.add("reg", registration); // add the register window to the layout

contentPre.add(deckPanel); // add the cards to the container

}

/**

* paint the buttons of the two models.

*

*/

public Box paintButtons()

{

/****************************************************

* Buttons

*****************************************************/

/**

* line1

*/

vPanel1 = Box.createVerticalBox();

// add the backwards's button

JButton backwards = new JButton("1/X");

backwards.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

backwards.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

backwards.addActionListener(this);

vPanel1.add(backwards);

// add the factorial button

JButton factorial = new JButton("X!");

factorial.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

factorial.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

factorial.addActionListener(this);

vPanel1.add(factorial);

// add the square's button

JButton square = new JButton("htmlXsup2/sup/html");

square.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

square.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

square.addActionListener(this);

square.setActionCommand("sqr");

vPanel1.add(square);

// add the square root's button

JButton squareRoot = new JButton("\u221a");

squareRoot.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

squareRoot.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

squareRoot.addActionListener(this);

vPanel1.add(squareRoot);

// add the power's button

JButton power = new JButton("htmlXsupY/sup/html");

power.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

power.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

power.addActionListener(this);

power.setActionCommand("pow");

vPanel1.add(power);

/**

* line2

*/

Box vPanel2 = Box.createVerticalBox();

// add the modular button

mod = new JButton("Mod");

mod.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

mod.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

mod.addActionListener(this);

vPanel2.add(mod);

// add the seven button

JButton seven = new JButton("7");

seven.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

seven.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

seven.addActionListener(this);

vPanel2.add(seven);

// add the four button

JButton four = new JButton("4");

four.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

four.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

four.addActionListener(this);

vPanel2.add(four);

// add the one button

JButton one = new JButton("1");

one.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

one.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

one.addActionListener(this);

vPanel2.add(one);

// add the zero button

JButton zero = new JButton("0");

zero.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

zero.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

zero.addActionListener(this);

vPanel2.add(zero);

/**

* line3

*/

Box vPanel3 = Box.createVerticalBox();

// add the Yth root of X button

xey = new JButton("XeY");

xey.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

xey.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

xey.addActionListener(this);

vPanel3.add(xey);

// add the eight button

JButton eight = new JButton("8");

eight.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

eight.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

eight.addActionListener(this);

vPanel3.add(eight);

// add the five button

JButton five = new JButton("5");

five.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

five.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

five.addActionListener(this);

vPanel3.add(five);

// add the two button

JButton two = new JButton("2");

two.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

two.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

two.addActionListener(this);

vPanel3.add(two);

// add the dot button

JButton dot = new JButton(".");

dot.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

dot.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

dot.addActionListener(this);

vPanel3.add(dot);

/**

* line4

*/

Box vPanel4 = Box.createVerticalBox();

// add the MS button

ms = new JButton("MS");

ms.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

ms.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

ms.addActionListener(this);

vPanel4.add(ms);

// add the nine button

JButton nine = new JButton("9");

nine.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

nine.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

nine.addActionListener(this);

vPanel4.add(nine);

// add the six button

JButton six = new JButton("6");

six.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

six.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

six.addActionListener(this);

vPanel4.add(six);

// add the three button

JButton three = new JButton("3");

three.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

three.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

three.addActionListener(this);

vPanel4.add(three);

// add the plusMinus button

JButton plusMinus = new JButton("\u00b1");

plusMinus.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

plusMinus.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

plusMinus.addActionListener(this);

vPanel4.add(plusMinus);

/**

* line5

*/

Box vPanel5 = Box.createVerticalBox();

// add the MR button

mr = new JButton("MR");

mr.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

mr.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

mr.addActionListener(this);

vPanel5.add(mr);

// add the division button

JButton division = new JButton("\u00F7");

division.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

division.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

division.addActionListener(this);

vPanel5.add(division);

// add the multiplication button

JButton multiplication = new JButton("\u00d7");

multiplication.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

multiplication.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

multiplication.addActionListener(this);

vPanel5.add(multiplication);

// add the subtract button

JButton subtract = new JButton("-");

subtract.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

subtract.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

subtract.addActionListener(this);

vPanel5.add(subtract);

// add the plus button

JButton plus = new JButton("+");

plus.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

plus.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

plus.addActionListener(this);

vPanel5.add(plus);

/**

* line6

*/

Box vPanel6 = Box.createVerticalBox();

// add the MC button

mc = new JButton("MC");

mc.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

mc.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

mc.addActionListener(this);

vPanel6.add(mc);

// add the C button

JButton c = new JButton("C");

c.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

c.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

c.addActionListener(this);

vPanel6.add(c);

// add a vertical strut

Component verticalStrut =

Box.createVerticalStrut(BUTTON_HEIGHT);

vPanel6.add(verticalStrut);

// add the enter button

JButton enter = new JButton("=");

enter.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT * 2));

enter.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT * 2));

enter.addActionListener(this);

vPanel6.add(enter);

/**

* Buttons panel

*/

Box buttonsPanel = Box.createHorizontalBox();

buttonsPanel.add(vPanel1);

buttonsPanel.add(vPanel2);

buttonsPanel.add(vPanel3);

buttonsPanel.add(vPanel4);

buttonsPanel.add(vPanel5);

buttonsPanel.add(vPanel6);

/**********************************************************

* the initial state is normal calculator

***********************************************************/

vPanel1.setVisible(false);

mod.setVisible(false);

xey.setVisible(false);

ms.setVisible(false);

mr.setVisible(false);

mc.setVisible(false);

return buttonsPanel;

}

/**

* paint the registration window.

*

*/

public Box paintRegistration()

{

Box registration = Box.createVerticalBox();

/*

* title

*/

JLabel titleRegistration = new JLabel("Bingbing's Calculator Registration");

registration.add(titleRegistration);

/*

* information

*/

JPanel information = new JPanel();

information.setLayout(new GridLayout(6, 2));

//Name

JLabel name = new JLabel("Name:");

nameField = new JTextField();

information.add(name);

information.add(nameField);

//Street Address

JLabel streetAddress = new JLabel("Street Address:");

streetAddressField = new JTextFi

帮忙解释这个拓扑排序的程序

#include stdio.h//呵呵,我也学了一回,原来是这么回事,有机会也要用用,

#include string.h

typedef struct node{//边接点

int adjvex;

struct node *next;

}EdgeNode;

typedef struct vnode{//顶点

int id;

EdgeNode *link;

}vnode,Adjlist[100];

typedef Adjlist LGraph;

typedef struct snode{//栈结点

int data;

struct snode *next;

}Link_Stack;

Link_Stack *top,*s;

void Push(Link_Stack **top,int x)//入栈

{

s=(Link_Stack*)malloc(sizeof(Link_Stack));

s-data=x;

s-next=(*top)-next;

(*top)-next=s;

}

void GetTop(Link_Stack **top,int *x)//出栈,取得栈顶元素

{

s=(*top)-next;

*x=s-data;

(*top)-next=s-next;

free(s);

}

int CreatGraph(LGraph gl)//创建图

{

int m,n,i=0,c,d;

EdgeNode *p;

printf("input start.\n");

printf("please input how many vertex there had?\n",i);

scanf("\n%d",c);

for(i=0;ic;i++){//初始化邻接表

gl[i].link=NULL;

gl[i].id=0;

}

printf("please input EdgeNodes number?");

scanf("%d",d);

for(i=0;id;i++){

printf("\nplease input ViVj:=");

scanf("\n%d,%d",m,n);

if((m=0)(mc)(n=0)(nc)){//验证m,n合法则输入,以建立邻接表

p=(EdgeNode*)malloc(sizeof(EdgeNode));

p-adjvex=n; //顶点

p-next=gl[m].link; //在相应位置插入邻接表

gl[m].link=p;

gl[n].id++;//顶点n入度+1

}

}

return c;

}

void TopuSort(LGraph G,int n)//拓扑排序

{

int i,j,m=0;

EdgeNode *p;

top=(Link_Stack*)malloc(sizeof(Link_Stack));

top-next=NULL;

for(i=0;in;i++)

if(G[i].id==0) Push(top,i);//入度(id)为零者入栈

while(top-next!=NULL){

GetTop(top,i);//老大是你出场的时候了

printf(" %d ",i);

m++;

p=G[i].link;

while(p){

j=p-adjvex;// 以P为起点的边的尾点入度-1

G[j].id--;

if(G[j].id==0) Push(top,j);//入度(id)为零者入栈

p=p-next;//下一条边

}

}

if(mn) printf("The Graph has a cycle!!!\n");

}

main(){

LGraph GL;

TopuSort(GL,CreatGraph(GL));

}

拓扑排序 编程

*/

#include stdio.h

#include malloc.h

// 输出有向图的一个拓扑序列。实现算法7.12的程序

// 图的邻接表存储表示

#define MAX_NAME 3 // 顶点字符串的最大长度+1

#define MAX_VERTEX_NUM 20

typedef int InfoType; // 存放网的权值

typedef char VertexType[MAX_NAME]; // 字符串类型

typedef enum{DG,DN,AG,AN}GraphKind; // {有向图,有向网,无向图,无向网}

typedef struct ArcNode

{

int adjvex; // 该弧所指向的顶点的位置

struct ArcNode *nextarc; // 指向下一条弧的指针

InfoType *info; // 网的权值指针)

}ArcNode; // 表结点

typedef struct VNode

{

VertexType data; // 顶点信息

ArcNode *firstarc; // 第一个表结点的地址,指向第一条依附该顶点的弧的指针

}VNode,AdjList[MAX_VERTEX_NUM];// 头结点

typedef struct

{

AdjList vertices;

int vexnum,arcnum; // 图的当前顶点数和弧数

int kind; // 图的种类标志

}ALGraph;

// 若G中存在顶点u,则返回该顶点在图中位置;否则返回-1。

int LocateVex(ALGraph G,VertexType u)

{

int i;

for(i=0;iG.vexnum;++i)

if(strcmp(u,G.vertices[i].data)==0)

return i;

return -1;

}

// 采用邻接表存储结构,构造没有相关信息的图G(用一个函数构造4种图)。

int CreateGraph(ALGraph *G)

{

int i,j,k;

int w; // 权值

VertexType va,vb;

ArcNode *p;

printf("请输入图的类型(有向图:0,有向网:1,无向图:2,无向网:3): ");

scanf("%d",(*G).kind);

printf("请输入图的顶点数和边数:(空格)\n");

scanf("%d%d", (*G).vexnum, (*G).arcnum);

printf("请输入%d个顶点的值(%d个字符):\n",(*G).vexnum,MAX_NAME);

for(i = 0; i (*G).vexnum; ++i) // 构造顶点向量

{

scanf("%s", (*G).vertices[i].data);

(*G).vertices[i].firstarc = NULL;

}

if((*G).kind == 1 || (*G).kind == 3) // 网

printf("请顺序输入每条弧(边)的权值、弧尾和弧头(以空格作为间隔):\n");

else // 图

printf("请顺序输入每条弧(边)的弧尾和弧头(以空格作为间隔):\n");

for(k = 0;k (*G).arcnum; ++k) // 构造表结点链表

{

if((*G).kind==1||(*G).kind==3) // 网

scanf("%d%s%s",w,va,vb);

else // 图

scanf("%s%s",va,vb);

i = LocateVex(*G,va); // 弧尾

j = LocateVex(*G,vb); // 弧头

p = (ArcNode*)malloc(sizeof(ArcNode));

p-adjvex = j;

if((*G).kind == 1 || (*G).kind == 3) // 网

{

p-info = (int *)malloc(sizeof(int));

*(p-info) = w;

}

else

p-info = NULL; // 图

p-nextarc = (*G).vertices[i].firstarc; // 插在表头

(*G).vertices[i].firstarc = p;

if((*G).kind = 2) // 无向图或网,产生第二个表结点

{

p = (ArcNode*)malloc(sizeof(ArcNode));

p-adjvex = i;

if((*G).kind == 3) // 无向网

{

p-info = (int*)malloc(sizeof(int));

*(p-info) = w;

}

else

p-info = NULL; // 无向图

p-nextarc = (*G).vertices[j].firstarc; // 插在表头

(*G).vertices[j].firstarc = p;

}

}

return 1;

}

// 输出图的邻接表G。

void Display(ALGraph G)

{

int i;

ArcNode *p;

switch(G.kind)

{

case DG: printf("有向图\n");

break;

case DN: printf("有向网\n");

break;

case AG: printf("无向图\n");

break;

case AN: printf("无向网\n");

}

printf("%d个顶点:\n",G.vexnum);

for(i = 0; i G.vexnum; ++i)

printf("%s ",G.vertices[i].data);

printf("\n%d条弧(边):\n", G.arcnum);

for(i = 0; i G.vexnum; i++)

{

p = G.vertices[i].firstarc;

while(p)

{

if(G.kind = 1) // 有向

{

printf("%s→%s ",G.vertices[i].data,

G.vertices[p-adjvex].data);

if(G.kind == DN) // 网

printf(":%d ", *(p-info));

}

else // 无向(避免输出两次)

{

if(i p-adjvex)

{

printf("%s-%s ",G.vertices[i].data,

G.vertices[p-adjvex].data);

if(G.kind == AN) // 网

printf(":%d ",*(p-info));

}

}

p=p-nextarc;

}

printf("\n");

}

}

// 求顶点的入度,算法7.12、7.13调用

void FindInDegree(ALGraph G,int indegree[])

{

int i;

ArcNode *p;

for(i=0;iG.vexnum;i++)

indegree[i]=0; // 赋初值

for(i=0;iG.vexnum;i++)

{

p=G.vertices[i].firstarc;

while(p)

{

indegree[p-adjvex]++;

p=p-nextarc;

}

}

}

typedef int SElemType; // 栈类型

#define STACK_INIT_SIZE 10 // 存储空间初始分配量

#define STACKINCREMENT 2 // 存储空间分配增量

// 栈的顺序存储表示 P46

typedef struct SqStack

{

SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL

SElemType *top; // 栈顶指针

int stacksize; // 当前已分配的存储空间,以元素为单位

}SqStack; // 顺序栈

// 构造一个空栈S。

int InitStack(SqStack *S)

{

// 为栈底分配一个指定大小的存储空间

(*S).base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));

if( !(*S).base )

exit(0); // 存储分配失败

(*S).top = (*S).base; // 栈底与栈顶相同表示一个空栈

(*S).stacksize = STACK_INIT_SIZE;

return 1;

}

// 若栈S为空栈(栈顶与栈底相同的),则返回1,否则返回0。

int StackEmpty(SqStack S)

{

if(S.top == S.base)

return 1;

else

return 0;

}

// 插入元素e为新的栈顶元素。

int Push(SqStack *S, SElemType e)

{

if((*S).top - (*S).base = (*S).stacksize) // 栈满,追加存储空间

{

(*S).base = (SElemType *)realloc((*S).base,

((*S).stacksize + STACKINCREMENT) * sizeof(SElemType));

if( !(*S).base )

exit(0); // 存储分配失败

(*S).top = (*S).base+(*S).stacksize;

(*S).stacksize += STACKINCREMENT;

}

*((*S).top)++=e;

// 这个等式的++ * 优先级相同,但是它们的运算方式,是自右向左

return 1;

}

// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回1;否则返回0。

int Pop(SqStack *S,SElemType *e)

{

if((*S).top == (*S).base)

return 0;

*e = *--(*S).top;

// 这个等式的++ * 优先级相同,但是它们的运算方式,是自右向左

return 1;

}

// 算法7.12 P182

// 有向图G采用邻接表存储结构。若G无回路,则输出G的顶点的一个拓扑序

// 列并返回1, 否则返回0。

int TopologicalSort(ALGraph G)

{

int i,k,count,indegree[MAX_VERTEX_NUM];

SqStack S;

ArcNode *p;

FindInDegree(G,indegree); // 对各顶点求入度indegree[0..vernum-1]

InitStack(S); // 初始化栈

for(i=0;iG.vexnum;++i) // 建零入度顶点栈S

if(!indegree[i])

Push(S,i); // 入度为0者进栈

count=0; // 对输出顶点计数

while(!StackEmpty(S))

{

// 栈不空

Pop(S,i);

printf("%s ",G.vertices[i].data); // 输出i号顶点并计数

++count;

for(p=G.vertices[i].firstarc;p;p=p-nextarc)

{

// 对i号顶点的每个邻接点的入度减1

k=p-adjvex;

if(!(--indegree[k])) // 若入度减为0,则入栈

Push(S,k);

}

}

if(countG.vexnum)

{

printf("此有向图有回路\n");

return 0;

}

else

{

printf("为一个拓扑序列。\n");

return 1;

}

}

int main()

{

ALGraph f;

printf("请选择有向图\n");

CreateGraph(f);

Display(f);

TopologicalSort(f);

system("pause");

return 0;

}

/*

输出效果:

请选择有向图

请输入图的类型(有向图:0,有向网:1,无向图:2,无向网:3): 0

请输入图的顶点数和边数:(空格)

4 4

请输入4个顶点的值(3个字符):

a

b

c

d

请顺序输入每条弧(边)的弧尾和弧头(以空格作为间隔):

a b

a c

b d

c d

有向图

4个顶点:

a b c d

4条弧(边):

a→c a→b

b→d

c→d

a b c d 为一个拓扑序列。

请按任意键继续. . .

*/

堆排序的代码(拓扑排序的伪代码)

额。。程序最后多打了一个大括号

这段代码的功能是对一个有向图进行拓扑排序,若有环则抛出异常

首先要明白拓扑排序是做什么的,详见百度百科--拓扑排序(有图有样例)

然后说这段代码:

通过将图中入度为0的点存入队列,用队首的点去查找与此点连接且入度为1的点,

把找到的点在存入队列,令这队首的点出队。如此至到找不到入度为0的点,结束while

循环,最后再判断进入再离开队列的点的个数若小于图中总点数,则判定有环。

注:可以根据注释判断函数功能

void Graph::topsort()

{

QueueVertex q;//定义存储点的信息的队列,由于是伪代码,信息类型不详,一般是该点在题目中的编号

int counter=0;//记录当前有几个点已经出队了

q.makeEmpty();//初始化队列,令其清空

for each Vertex v //初始化查找入度为0的点,若找到则入队

if(v.indegree==0) //判断入度为0

q.enqueue(v); //入队

while(!q.isEmpty()) //若队列不为空则一直循环

{

Vertex v=q.dequeue(); //队首点出队

v.topNum=++counter; //累加出队点数

for each Vertex w adjacent to v //用此出队点,即刚才的队首,查找仅与其相连的点,并将找到的点入队

if(--w.indegree==0)

q.enqueue(w);

}

if(counter!=NUM_VERTICES) //若总出队点不等于图中总点数,则有环

throw CycleFoudException();

}

求一个实现网络拓扑的JAVA程序

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

public class Io {

public static void main(String[] args) {

//没有用excel表格随便用了一个txt文本文档你参考下

File fl=new File("src/test.txt");//输出目的文件,没有文件会自动创建

try {

FileOutputStream out=new FileOutputStream(fl);//输出流

FileChannel Channel=out.getChannel();

ByteBuffer buff= ByteBuffer.allocate(100);//缓冲区长度

byte by[]="Hello 文本内容进行输出测试 ".getBytes();//设置内容

buff.put(by);//把内容放进缓冲区

buff.flip();

try {

Channel.write(buff);// 将字节序列从给定的缓冲区写入此通道

Channel.close();//关闭此通道

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

关于拓扑控制程序代码和拓扑控制算法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://www.ahzz.com.cn/post/6104.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载