الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

minimum spanning tree

بدأه ريحانة الاقصى في 31 مارس 2011 · 0 رد · 421 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1

السلام عليكم ورحمة الله وبركاته

اريد القيام ببرنامج يقوم بعمل implementation لخوارزمية ال minimum spanning tree وفي العادة تتم ب 3 خطوات

الاولى تمثيل الشكل من خلال مصفوفة ثنائية تمثل العناصر والاوزان التي عليها

ومن ثم ايجاد العناصر التي تشكل حلقة

والاخيرة ترتيب العناصر

في المرحلة الاولى عند ثمثيل البيانات من المستخدم في مصفوفة اريده تلقائيا ان يقوم بوضع نفس القيمة في مكانين مختلفين وهما المتعاكسان مثلا

arr[0][1]لها نفس قيمة arr[1][0]

استخدمت هذا الامر ولكن للاسف لا يعطي النتيجة

      public void format_array(){


   	 for (int i = 0; i <4; i++) {
   	 for (int A = 0; A < 4; A++) {

      textfields[A].setText(textfields[A].getText()); 


   }}
   }

فهل من الممكن المساعدة في هذا

وكيف اقوم بمعرفة العناصرة التي تشكل حلقات مثلا اذا قام المستخدم بادخال المسار AB وكان وزنه 7 انه يشكل حلقة او لا

حاولت في البرنامج اه يقوم بفحص المصفوفة لايجاد طريقة للوصول الى A من خلال B اذا نجح في الوصول تكون مغلقة ويرجع انها مغلقة او لا

    /**
 * @(#)MinmumSpaningTree.java
 *
 *
 * @author 
 * @version 1.00 2011/3/29
 */

import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MinmumSpaningTree2 extends JFrame implements ActionListener{

	JPanel contener =new JPanel(new BorderLayout()); 

	   String arr [][]=new String [4][4];
	   String code="";
       private JTextField[][] textfields;
       private JLabel[] Label1;
       private JLabel[] Label2;

        JPanel textFieldPanel=new JPanel(new GridLayout(4,4)); 


       	JPanel Label_horzintal=new JPanel(new GridLayout(1,4));  
        JPanel Label_vertical=new JPanel(new GridLayout(4,1));

        String s = "                                             APPLET FOR Minmum Spaning Tree           ";
        JLabel title = new JLabel(s);

        JTextArea edge=new JTextArea(10,30);
        JButton edgeButton=new JButton("        GET edge        ");
        JButton RESTARTButton=new JButton("        RESTART        ");



    public MinmumSpaningTree2() {
    	setLayout(new FlowLayout());
    	this.setLocation(100, 100);
        this.setTitle("insertion applet ");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(400, 300);
        Font f = new Font("SansSerif", Font.BOLD, 16);
        title.setBorder(new TitledBorder(""));
        title.setFont(f);

        edge.setBackground(new Color(240,238,240));
        edge.setFont(f);

        drow();
    	contener.add(Label_horzintal,BorderLayout.NORTH);
        contener.add(Label_vertical,BorderLayout.WEST);
    	contener.add(textFieldPanel,BorderLayout.CENTER);
    	add(title,BorderLayout.NORTH);
    	add(contener,BorderLayout.CENTER);
    	add(edge);
    	add(edgeButton);
    	add(RESTARTButton);

    	edgeButton.addActionListener(this);

    }

   public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(edgeButton)) {
   format_array();
           store_in_array();
         //  format_array();
           Read_from_array();

           edge.setText(code);

        }
   }


   public void Read_from_array(){

   	 for (int i = 0; i <4; i++) {
   	 for (int A = 0; A < 4; A++) {

   	     if((arr[A]).compareTo("0")!=0){

   	         code+=""+arr[A]+"\n";
   	     }


   	     }}
   }



   public void store_in_array(){

   	 for (int i = 0; i <4; i++) {
   	 for (int A = 0; A < 4; A++) {

   	 arr[A]=textfields[A].getText();	

   	     }}

   }

  public void format_array(){


   	 for (int i = 0; i <4; i++) {
   	 for (int A = 0; A < 4; A++) {

      textfields[A].setText(textfields[A].getText()); 


   }}
   }

    public static void main(String[] args) {
     MinmumSpaningTree2 minmumSpaningTree=new MinmumSpaningTree2();   
    }


    public  void drow(){

    	textfields = new JTextField[4][4];

   	for (int j = 0; j < textfields.length; j++) {

        for (int i = 0; i < textfields.length; i++) {


            JTextField field = new JTextField(6);

            textfields[j] = field;
           	if(i==j){

   	        arr[j]="0";
   	       textfields[j].setText("0");
   	   }

            textFieldPanel.add(field);
        }}



    	Label1 = new JLabel[4];
    	Label2 = new JLabel[4];
    	Label_horzintal.add(new JLabel("     "));
      for (int d = 0; d< Label1.length; d++) {

        JLabel lablel = new JLabel("    "+(char )(d+65)+"   ");
            Label1[d] = lablel;
            Label_horzintal.add(lablel);


    }   
     for (int f = 0; f < Label2.length; f++) {

        JLabel label2 = new JLabel("    "+(char )(f+65)+"   ");
            Label2[f] = label2;
            Label_vertical.add(label2);


    } 	

    } 	





}

مواضيع مشابهة