diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..73f69e0958611ac6e00bde95641f6699030ad235 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..639900d13c6182e452e33a3bd638e70a0146c785 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectRootManager"> + <output url="file://$PROJECT_DIR$/out" /> + </component> +</project> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..5d7df36ea6ff3f3c3f8db4122d04a6bffa2a7d83 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/Vektedegrafer/Vektedegrafer.iml" filepath="$PROJECT_DIR$/Vektedegrafer/Vektedegrafer.iml" /> + <module fileurl="file://$PROJECT_DIR$/.idea/oving7.iml" filepath="$PROJECT_DIR$/.idea/oving7.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/.idea/oving7.iml b/.idea/oving7.iml new file mode 100644 index 0000000000000000000000000000000000000000..d6ebd4805981b8400db3e3291c74a743fef9a824 --- /dev/null +++ b/.idea/oving7.iml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$" /> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000000000000000000000000000000000000..e96534fb27b68192f27f985d3879e173ec77adb8 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="Palette2"> + <group name="Swing"> + <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> + </item> + <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> + </item> + <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> + <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> + <initial-values> + <property name="text" value="Button" /> + </initial-values> + </item> + <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="RadioButton" /> + </initial-values> + </item> + <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="CheckBox" /> + </initial-values> + </item> + <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="Label" /> + </initial-values> + </item> + <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> + <preferred-size width="200" height="200" /> + </default-constraints> + </item> + <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> + <preferred-size width="200" height="200" /> + </default-constraints> + </item> + <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> + </item> + <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> + <preferred-size width="-1" height="20" /> + </default-constraints> + </item> + <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> + </item> + <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> + </item> + </group> + </component> +</project> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/Vektedegrafer/Vektedegrafer.iml b/Vektedegrafer/Vektedegrafer.iml new file mode 100644 index 0000000000000000000000000000000000000000..bbe128baab62f3cbbdaab9afd5e9f0ca953f0b6e --- /dev/null +++ b/Vektedegrafer/Vektedegrafer.iml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + </content> + <orderEntry type="jdk" jdkName="openjdk-17" jdkType="JavaSDK" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file diff --git a/Vektedegrafer/src/Graf.java b/Vektedegrafer/src/Graf.java new file mode 100644 index 0000000000000000000000000000000000000000..233927520e66fe1ac457de5c1bc611e31ffe1ef2 --- /dev/null +++ b/Vektedegrafer/src/Graf.java @@ -0,0 +1,149 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.StringTokenizer; +import java.util.*; + +public class Graf { + + + static class Kant { + int fra, til, flyt, kapasitet; + public Kant motsattKant; + + public Kant(int fra, int til, int kapasitet) { + this.fra = fra; + this.til = til; + this.kapasitet = kapasitet; + } + } + + static class Node { + ArrayList<Kant> kanter = new ArrayList<>(); + } + + static class MaksFlyt { + ArrayList<Svar> svar = new ArrayList<>(); // økning og flytøkende vei for graf + int maksFlyt; + + public static Node[] nyGraf(URL url) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); + int E = Integer.parseInt(st.nextToken()); + + Node[] noder = new Node[N]; + for (int i = 0; i < N; i++) { + noder[i] = new Node(); + } + + for (int i = 0; i < E; i++) { + st = new StringTokenizer(br.readLine()); + + int fra = Integer.parseInt(st.nextToken()); + int til = Integer.parseInt(st.nextToken()); + + int kapasitet = Integer.parseInt(st.nextToken()); + + if (kapasitet == 0) throw new IllegalArgumentException("Forward edge kapasitet <= 0"); + Kant e1 = new Kant(fra, til, kapasitet); + Kant e2 = new Kant(til, fra, 0); + e1.motsattKant = e2; + e2.motsattKant = e1; + noder[fra].kanter.add(e1); + noder[til].kanter.add(e2); + } + return noder; + } + + void edmondsKarp(int start, int slutt, Node[] graf) { + maksFlyt = 0; + while (true) { + Kant[] flowPath = new Kant[graf.length]; + Queue<Node> queue = new ArrayDeque<>(); + queue.add(graf[start]); + // BFS + while (!queue.isEmpty()) { + + Node current = queue.poll(); // fjerner og returnerer node i kø + for (Kant e : current.kanter) { + if (flowPath[e.til] == null && e.til != start && e.kapasitet > e.flyt) { + + // lagrer flowpath til bfs slik at man kan jobbe med det senere + flowPath[e.til] = e; + queue.add(graf[e.til]); + } + } + } + if (flowPath[slutt] == null) { + break; // enden ikke nådd + } + int pushFlow = Integer.MAX_VALUE; + for (Kant e = flowPath[slutt]; e != null; e = flowPath[e.fra]) + pushFlow = Math.min(pushFlow, e.kapasitet - e.flyt); + // lagrer svar fra BFS søk + Svar svar = new Svar(); + svar.okning = pushFlow; + svar.kanter = new LinkedList<>(); + svar.kanter.add(slutt); + // uppdaterer flow for kanter + for (Kant e = flowPath[slutt]; e != null; e = flowPath[e.fra]) { + e.flyt += pushFlow; + e.motsattKant.flyt -= pushFlow; + svar.kanter.addFirst(e.fra); + } + this.svar.add(svar); + maksFlyt += pushFlow; + } + } + + void printSvar() { + for (Svar value : svar) { + System.out.println(value); + } + } + + static class Svar { + int okning; + LinkedList<Integer> kanter; + + @Override + public String toString() { + return "Økning:"+ okning+ " | kanter: "+ kanter; + } + } + + public void grafAnalyse(String grafNavn, int start, int slutt, Node[] graf) { + MaksFlyt mf = new MaksFlyt(); + System.out.println(grafNavn); + mf.edmondsKarp(start, slutt, graf); + mf.printSvar(); + System.out.println("Max flyt " + mf.maksFlyt + "\n"); + } + + public static void main(String[] args) throws IOException { + MaksFlyt mf = new MaksFlyt(); +//Graf1 + URL url1 = new URL("http://www.iie.ntnu.no/fag/_alg/v-graf/flytgraf1"); + Node[] graf1 = MaksFlyt.nyGraf(url1); + String grafNavn1 = " Graf 1"; + mf.grafAnalyse(grafNavn1, 0, 7, graf1); +//graf 2 + URL url2 = new URL("http://www.iie.ntnu.no/fag/_alg/v-graf/flytgraf2"); + Node[] graf2 = MaksFlyt.nyGraf(url2); + String grafNavn2 = " Graf 2"; + mf.grafAnalyse(grafNavn2, 4, 15, graf2); +//graf 3 + URL url3 = new URL("http://www.iie.ntnu.no/fag/_alg/v-graf/flytgraf3"); + Node[] graf3 = MaksFlyt.nyGraf(url3); + String grafNavn3 = " Graf 3"; + mf.grafAnalyse(grafNavn3, 3, 15, graf3); + } + } +} + +