Skip to content

Commit

Permalink
refactored codes and changed node image
Browse files Browse the repository at this point in the history
  • Loading branch information
mssalkhalifah committed Mar 29, 2021
1 parent d17d117 commit 64ccdf6
Show file tree
Hide file tree
Showing 31 changed files with 274 additions and 276 deletions.
Binary file modified .gradle/6.7/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/6.7/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/6.7/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/6.7/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/6.7/javaCompile/classAnalysis.bin
Binary file not shown.
Binary file modified .gradle/6.7/javaCompile/javaCompile.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file modified build/classes/java/main/com/project/controller/EntryPoint.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/classes/java/main/com/project/utility/MyAlgorithm.class
Binary file not shown.
Binary file modified build/classes/java/main/com/project/view/MSTResultView.class
Binary file not shown.
Binary file not shown.
Binary file added build/resources/main/PC.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added build/resources/main/Redpc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 10 additions & 8 deletions build/tmp/compileJava/source-classes-mapping.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ com/project/view/ChartResultView.java
com.project.view.ChartResultView$dataCompareTypes
com/project/model/Subset.java
com.project.model.Subset
com/project/utility/MinimumSpanningTreePrim.java
com.project.utility.MinimumSpanningTreePrim
com/project/utility/MinimumSpanningTreeKruskal.java
com.project.utility.MinimumSpanningTreeKruskal
com/project/view/MSTResultView.java
com.project.view.MSTResultView
com/project/utility/SetUtil.java
com.project.utility.SetUtil
com/project/utility/GraphGeneratorWorker.java
com.project.utility.GraphGeneratorWorker
com/project/controller/Application.java
com.project.controller.Application
com/project/utility/GraphBenchmarkWorker.java
com.project.utility.GraphBenchmarkWorker
com/project/utility/MinimumSpanningTreePrim.java
com.project.utility.MinimumSpanningTreePrim
com/project/utility/MinimumSpanningTreeKruskal.java
com.project.utility.MinimumSpanningTreeKruskal
com/project/view/MSTResultView.java
com.project.view.MSTResultView
com/project/utility/MyAlgorithm.java
com.project.utility.MyAlgorithm
com.project.utility.MyAlgorithm$algorithms
com/project/view/MainView.java
com.project.view.MainView
com/project/controller/Application.java
com.project.controller.Application
com/project/controller/EntryPoint.java
com.project.controller.EntryPoint
205 changes: 43 additions & 162 deletions src/main/java/com/project/controller/Application.java
Original file line number Diff line number Diff line change
@@ -1,220 +1,101 @@
package com.project.controller;

import com.project.utility.GraphBenchmarkWorker;
import com.project.utility.MyAlgorithm;
import com.project.view.ChartResultView;
import com.project.view.MSTResultView;
import com.project.view.MainView;
import org.graphstream.graph.Graph;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class Application extends JFrame implements Runnable {
private MSTResultView kruskalResultView;
private MSTResultView primResultView;
private ChartResultView chartResultView;

private JTabbedPane resultTabbedPane;
private JPanel kruskalTab;
private JPanel primTab;
private JPanel kruskalGraphPanel;
private JPanel primGraphPanel;

private JTextField numberOfNodesPerStepTextField;
private JTextField numberOfStepsTextField;
private JTextField iterationsPerStepTextField;

private JButton generateButton;
private JButton clearButton;
private JButton runButton;

private boolean built;
public class Application {
private int numberOfVertexPerStep, maxNumberOfIteration, iterationPerStep;

@Override
public void run() {
System.setProperty("org.graphstream.ui", "swing");
this.setTitle("Minimum Spanning Network");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(1280, 720));

initLeftPane(this);

resultTabbedPane = new JTabbedPane();

this.getContentPane().add(resultTabbedPane);

this.setSize(this.getPreferredSize());
this.pack();
this.setVisible(true);
}

private void initLeftPane(Container container) {
JPanel topPanel = new JPanel();
public void start() {
MainView mainView = new MainView();

FlowLayout flowLayout = new FlowLayout();
flowLayout.setHgap(5);
mainView.display();

topPanel.setLayout(flowLayout);
topPanel.setBorder(BorderFactory.createLineBorder(Color.gray, 2));
mainView.getGenerateButton().addActionListener(event -> {
mainView.getGenerateButton().setEnabled(false);
mainView.getClearButton().setEnabled(true);
mainView.getRunButton().setEnabled(true);

topPanel.add(new JLabel("Nodes per step ="));
numberOfNodesPerStepTextField = new JTextField();
numberOfNodesPerStepTextField.setPreferredSize(new Dimension(70, 20));
topPanel.add(numberOfNodesPerStepTextField);

topPanel.add(new JLabel("Number of steps ="));
numberOfStepsTextField = new JTextField();
numberOfStepsTextField.setPreferredSize(new Dimension(70, 20));
topPanel.add(numberOfStepsTextField);

topPanel.add(new JLabel("Iteration per step ="));
iterationsPerStepTextField = new JTextField();
iterationsPerStepTextField.setPreferredSize(new Dimension(70, 20));
topPanel.add(iterationsPerStepTextField);

initButtons();

topPanel.add(generateButton);
topPanel.add(clearButton);
topPanel.add(runButton);

container.add(topPanel, BorderLayout.NORTH);
}

private void initGraphPanel() {
kruskalGraphPanel = new JPanel();
kruskalGraphPanel.setLayout(new GridLayout());
kruskalGraphPanel.setBorder(BorderFactory.createLineBorder(Color.gray, 2));

primGraphPanel = new JPanel();
primGraphPanel.setLayout(new GridLayout());
primGraphPanel.setBorder(BorderFactory.createLineBorder(Color.gray, 2));
}
mainView.initGraphPanel();

private void initButtons() {
Dimension dimension = new Dimension(100, 20);
this.numberOfVertexPerStep = Integer.parseInt(mainView.getNumberOfNodesPerStepTextField().getText());
this.maxNumberOfIteration = Integer.parseInt(mainView.getNumberOfStepsTextField().getText()) * numberOfVertexPerStep;
this.iterationPerStep = Integer.parseInt(mainView.getIterationsPerStepTextField().getText());

generateButton = new JButton("Generate");
generateButton.setPreferredSize(dimension);
mainView.getKruskalResultView().init(numberOfVertexPerStep, maxNumberOfIteration, iterationPerStep);
mainView.getPrimResultView().init(numberOfVertexPerStep, maxNumberOfIteration, iterationPerStep);

generateButton.addActionListener(event -> {
if (!built) {
generateButton.setEnabled(false);
clearButton.setEnabled(true);
runButton.setEnabled(true);
mainView.getKruskalResultView().execute();

initGraphPanel();

kruskalTab = new JPanel();
primTab = new JPanel();

kruskalTab.setLayout(new GridLayout());
primTab.setLayout(new GridLayout());

resultTabbedPane.addTab("Kruskal", kruskalTab);
resultTabbedPane.addTab("Prim", primTab);

kruskalTab.add(kruskalGraphPanel);
primTab.add(primGraphPanel);

kruskalResultView = new MSTResultView(kruskalGraphPanel, MyAlgorithm.algorithms.KRUSKAL);
primResultView = new MSTResultView(primGraphPanel, MyAlgorithm.algorithms.PRIM);
chartResultView = new ChartResultView(resultTabbedPane);

kruskalGraphPanel.add(kruskalResultView.getTabbedPane());
primGraphPanel.add(primResultView.getTabbedPane());

this.numberOfVertexPerStep = Integer.parseInt(numberOfNodesPerStepTextField.getText());
this.maxNumberOfIteration = Integer.parseInt(numberOfStepsTextField.getText()) * numberOfVertexPerStep;
this.iterationPerStep = Integer.parseInt(iterationsPerStepTextField.getText());

kruskalResultView.init(numberOfVertexPerStep, maxNumberOfIteration, iterationPerStep);
primResultView.init(numberOfVertexPerStep, maxNumberOfIteration, iterationPerStep);

kruskalResultView.execute();
try {
mainView.getKruskalResultView().get();
} catch (InterruptedException | ExecutionException interruptedException) {
interruptedException.printStackTrace();
}

if (mainView.getKruskalResultView().isDone()) {
mainView.getPrimResultView().execute();
System.out.println("Kruskal done!");
try {
kruskalResultView.get();
mainView.getPrimResultView().get();
} catch (InterruptedException | ExecutionException interruptedException) {
interruptedException.printStackTrace();
}

if (kruskalResultView.isDone()) {
primResultView.execute();
System.out.println("Kruskal done!");
try {
primResultView.get();
} catch (InterruptedException | ExecutionException interruptedException) {
interruptedException.printStackTrace();
}
}

built = true;
}
});

clearButton = new JButton("Clear");
clearButton.setPreferredSize(dimension);
mainView.getClearButton().addActionListener(event -> {
mainView.getGenerateButton().setEnabled(true);
mainView.getClearButton().setEnabled(false);
mainView.getRunButton().setEnabled(false);

clearButton.addActionListener(event -> {
generateButton.setEnabled(true);
clearButton.setEnabled(false);
runButton.setEnabled(false);

kruskalResultView.getGraphLists().stream()
mainView.getKruskalResultView().getGraphLists().stream()
.flatMap(Collection::parallelStream)
.forEach(Graph::clear);

primResultView.getGraphLists().stream()
mainView.getPrimResultView().getGraphLists().stream()
.flatMap(Collection::parallelStream)
.forEach(Graph::clear);

kruskalResultView.getSwingViewer().getGraphicGraph().clear();
primResultView.getSwingViewer().getGraphicGraph().clear();

resultTabbedPane.removeAll();
mainView.getKruskalResultView().getSwingViewer().getGraphicGraph().clear();
mainView.getPrimResultView().getSwingViewer().getGraphicGraph().clear();

built = false;
mainView.getResultTabbedPane().removeAll();
});

runButton = new JButton("Run");
runButton.setPreferredSize(dimension);

runButton.addActionListener(event -> {
generateButton.setEnabled(false);
clearButton.setEnabled(true);
runButton.setEnabled(false);
mainView.getRunButton().addActionListener(event -> {
mainView.getGenerateButton().setEnabled(false);
mainView.getClearButton().setEnabled(true);
mainView.getRunButton().setEnabled(false);

try {
GraphBenchmarkWorker graphBenchmarkWorker = new GraphBenchmarkWorker();

graphBenchmarkWorker.init(kruskalResultView.getGraphLists(), kruskalResultView.getAlgorithm());
graphBenchmarkWorker.init(mainView.getKruskalResultView().getGraphLists(), mainView.getKruskalResultView().getAlgorithm());
graphBenchmarkWorker.compute();

List<List<Double>> kruskalResults = graphBenchmarkWorker.getBenchmarkLists();

graphBenchmarkWorker.init(primResultView.getGraphLists(), primResultView.getAlgorithm());
graphBenchmarkWorker.init(mainView.getPrimResultView().getGraphLists(), mainView.getPrimResultView().getAlgorithm());
graphBenchmarkWorker.compute();

List<List<Double>> primResults = graphBenchmarkWorker.getBenchmarkLists();

chartResultView.draw(
mainView.getChartResultView().draw(
kruskalResults,
primResults,
numberOfVertexPerStep);

this.validate();
this.repaint();
mainView.validate();
mainView.repaint();
} catch (Exception e) {
e.printStackTrace();
}
});

clearButton.setEnabled(false);
runButton.setEnabled(false);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/project/controller/EntryPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public class EntryPoint {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Application());
SwingUtilities.invokeLater(new Application()::start);
}
}
53 changes: 24 additions & 29 deletions src/main/java/com/project/utility/GraphBenchmarkWorker.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.project.utility;

import org.graphstream.graph.Edge;
import org.graphstream.graph.Graph;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -24,48 +24,43 @@ public List<List<Double>> getBenchmarkLists() {
}

public void compute() {
MyAlgorithm myAlgorithm = null;

for (int i = 0; i < 1000; i++) {
myAlgorithm = (algorithms == MyAlgorithm.algorithms.KRUSKAL)
? new MinimumSpanningTreeKruskal()
: new MinimumSpanningTreePrim();
}
List<Double> runTimes = new LinkedList<>();
MyAlgorithm myAlgorithm;

for (List<Graph> graphList : graphLists) {
List<Double> runTimes = new LinkedList<>();

for (Graph graph : graphList) {
List<Double> temp = new LinkedList<>();

for (int i = 0; i < 130; i++) {
// Warm-up
for (int i = 0; i < 1000; i++) {
for (Graph graph : graphList) {
myAlgorithm = setup(algorithms);
myAlgorithm.init(graph);

double startTime = System.nanoTime();
myAlgorithm.compute();
double endTime = System.nanoTime();

temp.add((endTime - startTime));
}
}

myAlgorithm = setup(algorithms);

// Real test
for (Graph graph : graphList) {
myAlgorithm.init(graph);

double startTime = System.nanoTime();
myAlgorithm.compute();
double endTime = System.nanoTime();

double value = temp.stream().min(Double::compare).orElseThrow();
runTimes.add((endTime - startTime) /1000000);

runTimes.add(value/1000000);
myAlgorithm.getMstResult()
.forEach(edge -> edge.setAttribute("ui.style", "fill-color: blue; size: 5px;"));

List<Edge> mstArrayList = myAlgorithm.getMstResult();
mstArrayList.forEach(edge -> {
edge.setAttribute("ui.style", "fill-color: blue; size: 5px;");
edge.getNode0().setAttribute("ui.style", "fill-image: url('src/main/resources/PC.png');");
edge.getNode1().setAttribute("ui.style", "fill-image: url('src/main/resources/PC.png');");
});
myAlgorithm.getSuperComputers().setAttribute("ui.style", "fill-color: red; size: 4px;");
myAlgorithm.getSuperComputers().getNode0().setAttribute("ui.style","fill-image: url('src/main/resources/Redpc.png'); ");
myAlgorithm.getSuperComputers().getNode1().setAttribute("ui.style","fill-image: url('src/main/resources/Redpc.png'); ");
myAlgorithm.getSuperComputers().getNode0().setAttribute("ui.class", "superComputer");
myAlgorithm.getSuperComputers().getNode1().setAttribute("ui.class", "superComputer");
myAlgorithm.getSuperComputers().setAttribute("weight", myAlgorithm.getOriginalWeight());
}

benchmarkLists.add(runTimes);
benchmarkLists.add(new ArrayList<>(runTimes));
runTimes.clear();
}
}

Expand Down
Loading

0 comments on commit 64ccdf6

Please sign in to comment.