Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 44e2f10

Browse files
Graph based small project called as Air lines
1 parent 1318674 commit 44e2f10

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

‎README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ Please subscribe the channel: https://www.youtube.com/user/premaseemutube
2929
https://premaseem.wordpress.com/category/coding-challenge/
3030

3131
### Important link
32-
System Design Interview
32+
33+
#### Visualize algo and DS for easy understanding
34+
https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/visualize/
3335

3436
#### Github curated list
3537
https://github.com/yangshun/tech-interview-handbook/tree/master/experimental/design
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package me.premaseem.datastructure.graph;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
class City {
9+
int index;
10+
String name;
11+
String code;
12+
LinkedList<City> connectedCity = new LinkedList<>();
13+
14+
public City(int i, String name, String code) {
15+
index = i;
16+
this.name = name;
17+
this.code = code;
18+
}
19+
}
20+
21+
public class AirlineGraph {
22+
23+
int size =0;
24+
List<City> cities = new ArrayList<>();
25+
26+
void addCity(String code, String name){
27+
int i = size++;
28+
City city = new City(i,name, code);
29+
cities.add(city);
30+
}
31+
32+
City cityLookup(String code){
33+
for(City c:cities){
34+
if(c.code.equalsIgnoreCase(code)){
35+
return c;
36+
}
37+
}
38+
return null;
39+
}
40+
41+
void connectCity(String code1, String code2){
42+
City c1 = cityLookup(code1);
43+
City c2 = cityLookup(code2);
44+
if( c1 == null || c2 == null){
45+
System.out.println("Cannot connect, Invalid city code.");
46+
return;
47+
}
48+
c1.connectedCity.add(c2);
49+
}
50+
51+
52+
public static void main(String[] args) {
53+
54+
AirlineGraph g = new AirlineGraph();
55+
56+
// Add vertex or cities in the air line graph
57+
g.addCity("SFO","San Fransisco");
58+
g.addCity("AUS","Austin");
59+
g.addCity("NY","New york");
60+
g.addCity("BOM","Mumbai");
61+
g.addCity("IND","Indore");
62+
g.addCity("DEW","Dewas");
63+
64+
// Add edges or air ways in the graph
65+
g.connectCity("DEW","IND");
66+
g.connectCity("IND", "BOM");
67+
g.connectCity("BOM","NY");
68+
g.connectCity("BOM","SFO");
69+
g.connectCity("NY", "AUS");
70+
g.connectCity("NY", "SFO");
71+
g.connectCity("NY", "BOM");
72+
73+
// DFS to find path
74+
g.findDFSpath("DEW");
75+
g.findDFSpath("NY");
76+
77+
// BFS to find path
78+
g.findBFSpath("DEW");
79+
g.findBFSpath("NY");
80+
81+
82+
}
83+
84+
85+
private void findBFSpath(String code) {
86+
boolean[] visited = new boolean[size];
87+
City city = cityLookup(code);
88+
System.out.println("\n printing BFS path from city "+ city.name);
89+
LinkedList<City> q = new LinkedList<>();
90+
q.add(city);
91+
visited[city.index] = true;
92+
93+
while(!q.isEmpty()){
94+
City currentCity = q.poll();
95+
System.out.print(" >> "+ currentCity.name);
96+
97+
98+
for(City cc:currentCity.connectedCity){
99+
if(!visited[cc.index]){
100+
visited[cc.index] = true;
101+
q.add(cc);
102+
}
103+
}
104+
105+
106+
}
107+
108+
}
109+
110+
private void findDFSpath(String code) {
111+
boolean[] visited = new boolean[size];
112+
City city = cityLookup(code);
113+
System.out.println("printing DFS path from city "+ city.name);
114+
DFS(city, visited);
115+
}
116+
117+
private void DFS(City city, boolean[] visited) {
118+
visited[city.index] = true;
119+
for(City c :city.connectedCity){
120+
if(!visited[c.index]){
121+
DFS(c,visited);
122+
}
123+
}
124+
System.out.println(" >> "+city.name);
125+
}
126+
127+
128+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /