Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Java Weighted interval scheduling

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Weighted interval scheduling

    the Weighted Interval Scheduling problem is this: Given a set of weighted
    intervals, choose a set of non-overlapping intervals such that the total weight
    is maximal.

    These weighted intervals can be represented by the triples
    (0,3,3) (1,4,2) (0,5,4) (3,6,1) (4,7,2) (3,9,5) (5,10,2) (8,10,1)
    Write a program to compute a solution to the Weighted Interval Scheduling
    problem.
    Your program must read in a set of weighted intervals. Each interval should
    be entered as 3 integers. The intervals must be given in a textfile and also be
    entered in increasing order of finish time. In the above case we would have
    0 3 3 1 4 2 0 5 4 3 6 1 ...
    The program should print out the value of the total weight of the optimum
    solution and the indices of the selected intervals. In the above case, the output
    would be something like (this output is NOT correct)
    Optimum value: 7
    Interval Sequence: 2 5


    My text file is here: http://www.filedropper.com/weightedintervals

    pic: http://i.imgur.com/D6fcq.jpg

    I will have it to my box account soon. I can upload a archive file for eclipse if that helps you help me.

    My code as of right now always shows 8. Even if thats not the best solution.

    I need to print out the best job numbers also... the picture im displaying is the chart im going off of. Ill attach it... I want to take the jpanes and put them into a text box in the GUI. Its due thursday BTW!



    Here is my code so far:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Arrays;
    import java.util.Scanner;
    import java.awt.*;
    import javax.swing.*;
     
    public class weightedIntervals extends JFrame {
     
    	public void showImage() {
    		JFrame frame = new JFrame("My GUI");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(700, 400);
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
     
    		// Inserts the image icon
    		String imgStr = "Capture.jpg";
     
    		ImageIcon image = new ImageIcon(imgStr);
    		JLabel label1 = new JLabel(" ", image, JLabel.CENTER);
    		frame.getContentPane().add(label1);
     
    		frame.validate();
    		frame.setVisible(true);
     
    	}
     
    	public static void main(String[] args) throws FileNotFoundException {
    		int i, j;
    		String FileName;
    		FileName = "weightedIntervals.txt";
    		File f = new File(FileName);
    		Scanner fileIn = new Scanner(f);
    		int[] Start = new int[100];
    		int[] Finish = new int[100];
    		int[] Weight = new int[100];
    		i = 0;
    		while (fileIn.hasNext()) {
    			Start[i] = fileIn.nextInt();
    			Finish[i] = fileIn.nextInt();
    			Weight[i] = fileIn.nextInt();
    			i++;
    		}
    		i = 0;
    		while (i < 9) {
    			//System.out.println(Start[i] + ", " + Finish[i] + ", " + Weight[i]);
    			i++;
    		}
    		// just input
    		int Jobs[] = new int[8];
    		j = 0;
    		i = 0;
    		while (i != 7) {
    			Jobs[i] = i;
    			i++;
    		}
     
    		int M[] = new int[8];
    		int p[] = new int[8];
     
    		i = 0;
    		j = 0;
    		while (j != 8) {
    			i = 0;
    			while (i != 8) {
    				if (Start[j] >= Finish[i]) {
    					p[j] = Math.max(Finish[i], Start[j]);
    				}
    				i++;
    			}
    			j++;
    		}
     
    		j = 0;
    		while (j <= 7) {
    			if (j == 0) {
    				M[j] = 0;
    			}
    			if (j > 0) {
    				M[j] = Math.max(Weight[j] + Weight[p[j]], M[j - 1]);
    			}
    			j++;
    		}
     
    		JOptionPane.showMessageDialog(null,Arrays.toString(p) + "\n" + Arrays.toString(M));
     
    		JOptionPane.showMessageDialog(null, "Optimal Value: " + p[7]);
    		//JOptionPane.showMessageDialog(null, "Optimal Value: " + p[7]);
     
    		weightedIntervals show1 = new weightedIntervals();
    		show1.showImage();
    	}
     
    }


    I could also post an archive of my eclipse file if that helps
    Last edited by ryclegman; October 23rd, 2012 at 03:09 PM.


Similar Threads

  1. Scheduling algorithm
    By thesasantos in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 10th, 2012, 09:47 AM
  2. Scheduling Problem
    By pmg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 19th, 2011, 09:26 PM
  3. Help in Scheduling please????
    By touches in forum Algorithms & Recursion
    Replies: 2
    Last Post: October 20th, 2010, 07:41 AM
  4. non preemptive scheduling.....challenging!!
    By snehil2009 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 8th, 2009, 03:07 AM
  5. Replies: 1
    Last Post: March 18th, 2009, 06:21 AM