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

# Thread: Java Weighted interval scheduling

1. ## 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.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