1. Trang chủ
  2. » Công Nghệ Thông Tin

Xây dựng và thiết kế chương trình minh họa thuật toán Dijkstra

27 649 3
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Xây dựng và thiết kế chương trình minh họa thuật toán Dijkstra
Trường học University of Information Technology
Chuyên ngành Computer Science
Thể loại Đồ án tốt nghiệp
Thành phố Ho Chi Minh City
Định dạng
Số trang 27
Dung lượng 273,27 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Class Options chứa các Button để thực hiện việc vẽ các đỉnh, các cạnh chi phí, sửa, xóa, chạy thực thi thuật toán Dijkstra.private static final long serialVersionUID = 1L

Trang 1

1.Xây dựng và thiết kế chương trình minh họa thuật toán Dijkstra

1.1.Xây dựng chương trình cơ bản tìm đường đi ngắn nhất bằng thuật toán Dijkstra:

Viết bằng ngôn nữa C:

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define filein “Dijkstra.inp”

#define fileout “Dijkstra.out”

Trang 2

1.2.Thiết kế phần mềm minh họa thuật toán Dijkstra:

Sử dụng bộ công cụ Eclipse lập trình với Java.

1.2.a.Class Options:

Trang 3

Class Options chứa các Button để thực hiện việc vẽ các đỉnh, các cạnh (chi phí), sửa, xóa, chạy thực thi thuật toán Dijkstra.

private static final long serialVersionUID = 1L;

// Dat cac Options ben trai man hinh ung dung

Button b1 = new Button("Clear");

Button b2 = new Button("Run");

Button b3 = new Button("Step");

Button b4 = new Button("Reset");

Button b5 = new Button("Example");

Button b6 = new Button("Exit");

Button b7 = new Button("About");

public boolean action(Event evt, Object arg) {

if (evt.target instanceof Button) {

//Neu la JButton "Step" va Unlock thi thuc hien buoc dau tien

va hien thi "Nextstep" len JButton,

//nguoc lai thi khong thuc hien

if (((String)arg).equals("Step")) {

if (!Locked) {

parent.graphcanvas.stepalg();

else parent.documentation.doctext.showline("locked");

//Neu la JButton "Nextstep" thi thuc hien buoc tiep thep

if (((String)arg).equals("Next step")){

parent.graphcanvas.nextstep();

Trang 4

parent.graphcanvas.runalg();

else parent.documentation.doctext.showline("locked");

parent.graphcanvas.showexample();

else parent.documentation.doctext.showline("locked");

Trang 5

class Documentation extends Panel {

// Documentaion huong dan o phia tren man hinh ung dung

DocOptions docopt = new DocOptions(this);

DocText doctext = new DocText();

Documentation() {

setLayout(new BorderLayout(10, 10));

add("West", docopt);

add("Center", doctext);

}

}

class DocOptions extends Panel {

//Tao Choice huong dan ben trai man hinh ung dung

Choice doc = new Choice();

add(new Label("DOCUMENTATION:"));

doc.addItem("draw nodes");

doc.addItem("remove nodes");

doc.addItem("move nodes");

doc.addItem("the startnode");

doc.addItem("draw arrows");

doc.addItem("change weights");

doc.addItem("remove arrows");

doc.addItem("clear / reset");

doc.addItem("run algorithm");

doc.addItem("step through");

doc.addItem("example");

doc.addItem("about");

doc.addItem("exit");

doc.addItem("all items");

add(doc);

}

Trang 6

public boolean action(Event evt, Object arg) {

//Ham xu ly Choice

if (evt.target instanceof Choice) {

String str=new String(doc.getSelectedItem());

parent.doctext.showline(str);

}

}

}

class DocText extends TextArea {

//TextDocumentation huong dan cho cac Choice va JButton

final String drawnodes = new String("DRAWING NODES:\n"+

"Draw a node by clicking the mouse on the screen application.\n\n");

final String rmvnodes = new String("REMOVE NODES:\n"+

"To remove a node press <ctrl> and click on the node.\n"+

"You can not remove the startnode.\n"+

"Select another startnode, then you can remove the node.\n\n");

final String mvnodes = new String("MOVING NODES\n"+

"To move a node press <Shift>, click on the node,\nand drag it to"+ " its new position.\n\n");

final String startnode = new String("STARTNODE:\n"+

"The startnode is blue, other nodes are grey.\n"+

"The first node you draw on the screen will be the startnode.\n"+ "To select another startnode press <ctrl>, click on the startnode,\n"+

"and drag the mouse to another node.\n"+

"To delete the startnode, first select another startnode, and then"+

"\nremove the node the usual way.\n\n");

final String drawarrows = new String("DRAWING ARROWS:\n"+

"To draw an arrow click mouse in a node,"+

"and drag it to another node.\n\n");

final String weight = new String("CHANGING WEIGHTS:\n"+

"To change the weight of an arrow, click on the arrowhead and drag\n"+

"it along the arrow.\n\n");

final String rmvarrows = new String("REMOVE ARROWS:\n"+

"To remove an arrow, change its weight to 0.\n\n");

final String clrreset = new String("<CLEAR> BUTTON: "+

"Remove the current graph from the screen.\n"+

"<RESET> BUTTON: "+

"Remove the results of the algorithm from the graph,\n"+

" and unlock screen.\n\n");

final String runalg = new String("<RUN> BUTTON: "+

"Run the algorithm on the graph, there will be a time\n" +

"delay of +/- 1 second between steps.\n"+

Trang 7

"To run the algorithm slower, use <STEP>.\n");

final String step = new String("<STEP> BUTTON: " +

"An opportunity to step through the algorithm.\n");

final String example = new String("<EXAMPLE> BUTTON: "+

"Displays a default graph on the screen for you.\n"+

"You can then use <STEP> or <RUN>\n");

final String exitbutton = new String("<EXIT> BUTTON: " +

"Only works if applet is run with appletviewer.\n");

final String aboutbutton = new String("<ABOUT> BUTTON: " +

"Show about Dijkstra program information.\n");

final String toclose = new String("ERROR: "+

"This position is to close to another node/arrow.\n");

final String done = new String("Algorithm has finished, " +

"follow green arrows from startnode to any node "+

"to get\nthe shortest path to " +

"the node The length of the path is written in the node.\n" + "press <RESET> to reset the graph, and unlock the screen.");

final String some = new String("Algorithm has finished, " +

"follow green arrows from startnode to any node "+

"to get\nthe shortest path to " +

"the node The length of the path is written in the node.\n" + "There are no paths from the startnode to any gray node.\n" + "press <RESET> to reset the graph, and unlock the screen.");

final String none = new String("Algorithm has finished, " +

"there are no nodes reachable from the start node.\n"+

"press <RESET> to reset the graph, and unlock the screen.");

final String maxnodes = new String("ERROR: "+

"Maximum number of nodes reached!\n\n");

final String info = new String("DOCUMENTATION:\n"+

"You can scroll through the documentation or get documentation\n"+ "on a specific "+

"item by selecting the item on the left.\nSelecting <All items> "+ "brings you back "+

" to the scrolling text.\n\n");

final String locked = new String("ERROR: "+

"Keyboard/mouse locked for this action.\n"+

"Either press <NEXT STEP> or <RESET>.\n");

final String doc = info + drawnodes + rmvnodes + mvnodes +

startnode + drawarrows + weight + rmvarrows + clrreset + runalg + step + example + aboutbutton +

exitbutton;

DocText() {

super(5, 2);

setEditable(false);

Trang 8

}

public void showline(String str) {

if (str.equals("draw nodes")) setText(drawnodes);

else if (str.equals("remove nodes")) setText(rmvnodes);

else if (str.equals("move nodes")) setText(mvnodes);

else if (str.equals("the startnode")) setText(startnode);

else if (str.equals("draw arrows")) setText(drawarrows);

else if (str.equals("change weights")) setText(weight);

else if (str.equals("remove arrows")) setText(rmvarrows);

else if (str.equals("clear / reset")) setText(clrreset);

else if (str.equals("run algorithm")) setText(runalg);

else if (str.equals("step through")) setText(step);

else if (str.equals("example")) setText(example);

else if (str.equals("exit")) setText(exitbutton);

else if (str.equals("about")) setText(aboutbutton);

else if (str.equals("all items")) setText(doc);

else if (str.equals("toclose")) setText(toclose);

else if (str.equals("done")) setText(done);

else if (str.equals("locked")) setText(locked);

else if (str.equals("maxnodes")) setText(maxnodes); else if (str.equals("none")) setText(none);

else if (str.equals("some")) setText(some);

JPanel jp1=new JPanel();

JPanel jp2=new JPanel(new GridLayout(3,1));

Trang 9

JPanel jp3=new JPanel();

ImageIcon img=new

ImageIcon(this.getClass().getResource("/nhanntv/nhanntv1.png"));

//tao JLabel

JLabel jlb1=new JLabel(img);

JLabel jlb2=new JLabel("************Program Java to************");JLabel jlb3=new JLabel("****Dijkstra Algorithm Program Demo****");JLabel jlb4=new JLabel("***CopyWrite @2011 Edit by Nhanntv***");

//Tao Layout

GridBagLayout gb=new GridBagLayout();

GridBagConstraints gbc=new GridBagConstraints();

private static final long serialVersionUID = 1L;

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getSource()==jb){

this.dispose();

}}

public void addComponent(Component c,int row,int col,int nrow,int

ncol){

gbc.gridy=row;// toa do y

gbc.gridx=col;// toa do x

gbc.gridheight=nrow;// so dong chiem

gbc.gridwidth=ncol;// so cot chiem

Trang 10

Class GraphCanvas là Class đồ họa của chương trình, chứa các thuật toán giải quyết việc tìm đường đi ngắn nhất trong mạng (Shortest Path Routing), các phương thức để vẽ một mạng, xây dựng và tìm đường, đưa ra kết quả dưới dạng đồ họa cho giải thuật Dijkstra.

class GraphCanvas extends Canvas implements Runnable {

// drawing area for the graph

final int MAXNODES = 20;

final int MAX = MAXNODES+1;

final int NODESIZE = 26;

final int NODERADIX = 13;

final int DIJKSTRA = 1;

// basic graph information

Point node[] = new Point[MAX]; // node

int weight[][] = new int[MAX][MAX]; // weight of arrow

Point arrow[][] = new Point[MAX][MAX]; // current position of

arrowhead

Point startp[][] = new Point[MAX][MAX]; // start and

Point endp[][] = new Point[MAX][MAX]; // endpoint of arrow

float dir_x[][] = new float[MAX][MAX]; // direction of arrow

float dir_y[][] = new float[MAX][MAX]; // direction of arrow

// graph information while running algorithm

boolean algedge[][] = new boolean[MAX][MAX];

int dist[] = new int[MAX];

int finaldist[] = new int[MAX];

Color colornode[] = new Color[MAX];

boolean changed[] = new boolean[MAX]; // indicates distance change during algorithm

// information used by the algorithm to find the next

// node with minimum distance

int mindist, minnode, minstart, minend;

int numnodes=0; // number of nodes

int emptyspots=0; // empty spots in array node[] (due to node deletion)

Trang 11

int startgraph=0; // start of graph

int hitnode; // mouse clicked on or close to this node

int node1, node2; // numbers of nodes involved in current action

Point thispoint=new Point(0,0); // current mouseposition

Point oldpoint=new Point(0, 0); // previous position of node being moved

// current action

boolean newarrow = false;

boolean movearrow = false;

boolean movestart = false;

boolean deletenode = false;

boolean movenode = false;

boolean performalg = false;

boolean clicked = false;

// fonts

Font roman= new Font("TimesRoman", Font.BOLD, 12);

Font helvetica= new Font("Helvetica", Font.BOLD, 15);

FontMetrics fmetrics = getFontMetrics(roman);

int = (int)fmetrics.getHeight()/3;

// for double buffering

private Image offScreenImage;

private Graphics offScreenGraphics;

private Dimension offScreenSize;

// for run option

Thread algrthm;

// current algorithm, (in case more algorithms are added)

int algorithm;

// algorithm information to be displayed in documetation panel

String showstring = new String("");

boolean stepthrough=false;

// locking the screen while running the algorithm

boolean Locked = false;

public void lock() {

// lock screen while running an algorithm

Locked=true;

}

public void unlock() {

Locked=false;

Trang 12

}

public void start() {

if (algrthm != null) algrthm.resume () }

public void init() {

for (int i=0;i<MAXNODES;i++) {

colornode[i]=Color.gray;

for (int j=0; j<MAXNODES;j++)

algedge[i][j]=false;

}

colornode[startgraph]=Color.blue;

performalg = false;

}

public void clear() {

// removes graph from screen

startgraph=0;

numnodes=0;

emptyspots=0;

init();

for(int i=0; i<MAXNODES; i++) {

node[i]=new Point(0, 0);

for (int j=0; j<MAXNODES;j++)

public void reset() {

// resets a graph after running an algorithm

public void runalg() {

// gives an animation of the algorithm

public void stepalg() {

// lets you step through the algorithm

Trang 13

for(int i=0; i<MAXNODES; i++) {

dist[i]=-1;

finaldist[i]=-1;

for (int j=0; j<MAXNODES;j++)

algedge[i][j]=false;

public void nextstep() {

// calculates a step in the algorithm (finds a shortest

// path to a next node)

finaldist[minend]=mindist;

algedge[minstart][minend]=true;

colornode[minend]=Color.green;

// build more information to display on documentation panel

step++;

repaint();

}

public void stop() {

if (algrthm != null) algrthm.suspend ()

}

public void run() {

for(int i=0; i<(numnodes-emptyspots); i++){

public void showexample() {

// draws a default graph on the screen

for(int i=0; i<MAXNODES; i++) {

node[i]=new Point(0, 0);

for (int j=0; j<MAXNODES;j++)

weight[i][j]=0;

w=this.size () width/8;

h=this.size () height/8;

node[0]=new Point(2*w, 4*h);

node[1]=new Point(3*w, 2*h);

node[2]=new Point(5*w, 2*h);

node[3]=new Point(6*w, 4*h);

node[4]=new Point(5*w, 6*h);

node[5]=new Point(3*w, 6*h);

weight[0][1]=4; weight[1][0]=4; weight[0][5]=2; weight[5][0]=2; weight[1][2]=3; weight[2][1]=3; weight[1][4]=3; weight[4][1]=3; weight[2][3]=2; weight[3][2]=2;

Trang 14

weight[3][4]=1; weight[4][3]=1;

weight[4][5]=3; weight[5][4]=3;

for (int i=0;i<numnodes;i++)

for (int j=0;j<numnodes;j++)

thispoint = new Point(x,y);

colornode[startgraph]=Color.gray;

else if (nodehit(x, y, NODESIZE)) {

// draw a new arrow

else if ( !nodehit(x, y, 50) && !arrowhit(x, y, 50) ) {

// draw new node

if (emptyspots==0) {

// take the next available spot in the array

if (numnodes < MAXNODES)

node[numnodes++]=new Point(x, y);

else parent.documentation.doctext.showline("maxnodes"); }

Ngày đăng: 25/10/2013, 03:20

HÌNH ẢNH LIÊN QUAN

Kết quả hiển thị lên màn hình là đồ thị đường đi ngắn nhất trong mạng với đỉnh xuất phát (màu xanh dương) và các đỉnh còn lại màu xanh lá cây - Xây dựng và thiết kế chương trình minh họa thuật toán Dijkstra
t quả hiển thị lên màn hình là đồ thị đường đi ngắn nhất trong mạng với đỉnh xuất phát (màu xanh dương) và các đỉnh còn lại màu xanh lá cây (Trang 26)

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w