forked from ReciHub/FunnyAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ReciHub#404 from mostefa-laoumir/seven-bridges-pro…
…blem-java added new folder called java and added solution of (the seven bridges of königsberg) problem in it
- Loading branch information
Showing
13 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
this is a simple java solution of the famous "The seven bridges of K�nigsberg" problem written by me | ||
|
||
|
||
|
||
thank you! | ||
by mostefa_laoumir |
6 changes: 6 additions & 0 deletions
6
java/The-seven-bridges-of-Königsberg/THG_Project/.idea/misc.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
java/The-seven-bridges-of-Königsberg/THG_Project/.idea/modules.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
java/The-seven-bridges-of-Königsberg/THG_Project/.idea/workspace.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
java/The-seven-bridges-of-Königsberg/THG_Project/THG_Project.iml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?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="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> | ||
|
Binary file added
BIN
+367 Bytes
java/The-seven-bridges-of-Königsberg/THG_Project/out/production/THG_Project/Bridge.class
Binary file not shown.
Binary file added
BIN
+4.49 KB
java/The-seven-bridges-of-Königsberg/THG_Project/out/production/THG_Project/Main.class
Binary file not shown.
Binary file added
BIN
+503 Bytes
java/The-seven-bridges-of-Königsberg/THG_Project/out/production/THG_Project/Vertige.class
Binary file not shown.
Binary file added
BIN
+588 Bytes
java/The-seven-bridges-of-Königsberg/THG_Project/out/production/THG_Project/dup.class
Binary file not shown.
9 changes: 9 additions & 0 deletions
9
java/The-seven-bridges-of-Königsberg/THG_Project/src/Bridge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public class Bridge { | ||
|
||
public Vertige v1, v2; | ||
public Bridge(Vertige v1, Vertige v2){ | ||
|
||
this.v1 = v1; | ||
this.v2 = v2; | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
java/The-seven-bridges-of-Königsberg/THG_Project/src/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import java.util.Scanner; | ||
import java.util.Vector; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
Vector<Vertige> vtgs = new Vector<>(); | ||
Vector<Bridge> bdgs = new Vector<>(); | ||
Scanner scn = new Scanner(System.in); | ||
System.out.println("Donner le nombre de vertiges : "); | ||
int n = scn.nextInt(); | ||
Vertige v; | ||
String nm; | ||
for(int i = 0 ; i<n;i++){ | ||
System.out.println("donner le nom du vertige num "+(i+1)+" : "); | ||
nm = scn.next(); | ||
v = new Vertige(nm); | ||
vtgs.add(v); | ||
} | ||
System.out.println("donner le nombres de ponts : "); | ||
int ponts = scn.nextInt(); | ||
Vertige v1 = new Vertige("vide"); | ||
v = new Vertige("vide"); | ||
Bridge b; | ||
String nm2, nm3 = ""; | ||
for(int i = 0; i<ponts;i++){ | ||
System.out.println("pont "+(i+1)+" : "); | ||
System.out.println("donner le nom du premiere vertige reliée : "); | ||
nm2 = ""; | ||
nm3 = ""; | ||
nm2 = scn.next(); | ||
for(int j = 0 ;j<n;j++){ | ||
if (vtgs.elementAt(j).nom.equals(nm2)){ | ||
v = new Vertige(vtgs.elementAt(j)); | ||
vtgs.elementAt(j).path++; | ||
|
||
} | ||
} | ||
System.out.println("donner le nom du deuxieme vertige reliée : "); | ||
do{ | ||
nm3 = scn.next(); | ||
}while(nm3.equals(nm2)); | ||
for(int j = 0 ;j<n;j++){ | ||
if (vtgs.elementAt(j).nom.equals(nm3)){ | ||
v1 = new Vertige(vtgs.elementAt(j)); | ||
vtgs.elementAt(j).path++; | ||
} | ||
} | ||
b = new Bridge(v,v1); | ||
bdgs.add(b); | ||
} | ||
|
||
|
||
|
||
|
||
/*for(int i = 0 ; i<n; i++){ | ||
System.out.println(vtgs.elementAt(i).nom+ " : " +vtgs.elementAt(i).path); | ||
}*/ | ||
boolean works = true; | ||
|
||
for(int j = 0 ; j<n; j++){ | ||
if(vtgs.elementAt(1).path!=vtgs.elementAt(j).path)works=false; | ||
} | ||
if(!works){ | ||
int t[][] = new int[2][n]; | ||
for(int i = 0 ; i<n; i++){ | ||
t[0][i] = vtgs.elementAt(i).path; | ||
t[1][i]=0; | ||
} | ||
for(int i = 0 ; i<n; i++){ | ||
for(int j = 0 ; j<n; j++){ | ||
if(t[0][i]==t[0][j])t[1][i]++; | ||
} | ||
} | ||
int k = dup.supprimer_dup(t[1],n); | ||
|
||
for(int i = 0; i<k;i++){ | ||
if(t[1][i]==2)works=true; | ||
} | ||
|
||
} | ||
if(works){ | ||
System.out.println("Chemin Trouvé"); | ||
}else{ | ||
System.out.println("Chemin Non Trouvé"); | ||
System.out.println("voulez vous ajouter ou supprimer un pont? A/S"); | ||
String choix = scn.next(); | ||
if(choix.equals("A") || choix.equals("a")){ | ||
ponts++; | ||
System.out.println("pont "+(ponts)+" : "); | ||
System.out.println("donner le nom du premiere vertige reliée : "); | ||
nm2 = ""; | ||
nm3 = ""; | ||
nm2 = scn.next(); | ||
for(int j = 0 ;j<n;j++){ | ||
if (vtgs.elementAt(j).nom.equals(nm2)){ | ||
v = new Vertige(vtgs.elementAt(j)); | ||
vtgs.elementAt(j).path++; | ||
|
||
} | ||
} | ||
System.out.println("donner le nom du deuxieme vertige reliée : "); | ||
do{ | ||
nm3 = scn.next(); | ||
}while(nm3.equals(nm2)); | ||
for(int j = 0 ;j<n;j++){ | ||
if (vtgs.elementAt(j).nom.equals(nm3)){ | ||
v1 = new Vertige(vtgs.elementAt(j)); | ||
vtgs.elementAt(j).path++; | ||
} | ||
} | ||
b = new Bridge(v,v1); | ||
bdgs.add(b); | ||
}else if(choix.equals("s") || choix.equals("S")){ | ||
System.out.println("donner les vertiges qui se relient par le pont : "); | ||
String p1 = scn.next(); | ||
String p2 = scn.next(); | ||
for(int m = 0;m<ponts;m++){ | ||
if(bdgs.elementAt(m).v1.nom.equals(p1) && bdgs.elementAt(m).v2.nom.equals(p2)){ | ||
bdgs.remove(m);break; | ||
} | ||
} | ||
for(int q = 0; q<n;q++){ | ||
if(vtgs.elementAt(q).nom.equals(p1) || vtgs.elementAt(q).nom.equals(p2))vtgs.elementAt(q).path--; | ||
} | ||
ponts--; | ||
} | ||
works = true; | ||
|
||
for(int j = 0 ; j<n; j++){ | ||
if(vtgs.elementAt(1).path!=vtgs.elementAt(j).path)works=false; | ||
} | ||
if(!works){ | ||
int t[][] = new int[2][n]; | ||
for(int i = 0 ; i<n; i++){ | ||
t[0][i] = vtgs.elementAt(i).path; | ||
t[1][i]=0; | ||
} | ||
for(int i = 0 ; i<n; i++){ | ||
for(int j = 0 ; j<n; j++){ | ||
if(t[0][i]==t[0][j])t[1][i]++; | ||
} | ||
} | ||
int k = dup.supprimer_dup(t[1],n); | ||
|
||
for(int i = 0; i<k;i++){ | ||
if(t[1][i]==2)works=true; | ||
} | ||
|
||
} | ||
if(works){ | ||
System.out.println("Chemin Trouvé!!!"); | ||
}else{ | ||
System.out.println("Chemin Non Trouvé!"); } | ||
} | ||
|
||
|
||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
java/The-seven-bridges-of-Königsberg/THG_Project/src/Vertige.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public class Vertige { | ||
public String nom; | ||
int path=0; | ||
public Vertige(String nom){ | ||
this.nom = nom; | ||
|
||
|
||
} | ||
public Vertige(Vertige v){ | ||
this.nom = v.nom; | ||
this.path = v.path; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
java/The-seven-bridges-of-Königsberg/THG_Project/src/dup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
public class dup { | ||
public static int supprimer_dup(int arr[], int n) { | ||
if (n == 0 || n == 1) | ||
return n; | ||
|
||
int[] temp = new int[n]; | ||
int j = 0; | ||
for (int i = 0; i < n - 1; i++) | ||
if (arr[i] != arr[i + 1]) | ||
temp[j++] = arr[i]; | ||
|
||
temp[j++] = arr[n - 1]; | ||
|
||
// Changing original array | ||
for (int i = 0; i < j; i++) | ||
arr[i] = temp[i]; | ||
|
||
return j; | ||
} | ||
} |