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
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit f8fb39f

Browse files
Node classess added for graph theory simulation
Added node classess for graph theory problems. Also added BFS and DFS implementation. Currently working on a euler tree implementation.
1 parent 2f93f99 commit f8fb39f

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

‎other/VisitListener.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
public interface VisitListener {
3+
public void onVisit(node n);
4+
}

‎other/bfs.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.*;
2+
public class bfs implements VisitListener{
3+
public void onVisit(node n) {
4+
// Do nothing
5+
System.out.println(n);
6+
}
7+
public Queue<node> q = new LinkedList<node>();
8+
public ArrayList<node> vn = new ArrayList<node>();
9+
public ArrayList<node> search(node startNode,VisitListener search) {
10+
this.q.add(startNode);
11+
this.vn.add(startNode);
12+
node cur;
13+
while(!(q.isEmpty())) {
14+
cur = this.q.poll();
15+
if(cur.isvisited()) {
16+
continue;
17+
}
18+
cur.visit();
19+
search.onVisit(cur);
20+
for(node sub:cur.connectedNodes) {
21+
q.add(sub);
22+
vn.add(sub);
23+
}
24+
}
25+
return vn;
26+
}
27+
public ArrayList<node> search(node startNode) {
28+
return this.search(startNode, this);
29+
}
30+
public static void main(String[] args) {
31+
node a,b,c,d,e,f,g;
32+
a = (new node()).setid(1);
33+
b = (new node()).setid(2);
34+
c = (new node()).setid(3);
35+
a.linkNode(b);
36+
a.linkNode(c);
37+
d = (new node()).setid(4);
38+
e = (new node()).setid(5);
39+
f = (new node()).setid(6);
40+
g = (new node()).setid(7);
41+
b.linkNode(d);
42+
b.linkNode(e);
43+
c.linkNode(f);
44+
c.linkNode(g);
45+
(new bfs()).search(a);
46+
}
47+
48+
}

‎other/dfs.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
public class dfs implements VisitListener{
3+
public void onVisit(node n) {
4+
// Do nothing
5+
System.out.println(n.getId());
6+
}
7+
public Stack<node> q = new Stack<node>();
8+
public ArrayList<node> vn = new ArrayList<node>();
9+
public ArrayList<node> search(node startNode,VisitListener search) {
10+
this.q.add(startNode);
11+
this.vn.add(startNode);
12+
node cur;
13+
while(!(q.isEmpty())) {
14+
cur = this.q.pop();
15+
if(cur.isvisited()) {
16+
continue;
17+
}
18+
cur.visit();
19+
search.onVisit(cur);
20+
21+
Collections.reverse(cur.connectedNodes);
22+
for(node sub:cur.connectedNodes) {
23+
q.add(sub);
24+
vn.add(sub);
25+
}
26+
}
27+
return vn;
28+
}
29+
public ArrayList<node> search(node startNode) {
30+
return this.search(startNode, this);
31+
}
32+
public static void main(String[] args) {
33+
node a,b,c,d,e,f,g;
34+
a = (new node()).setid(1);
35+
b = (new node()).setid(2);
36+
c = (new node()).setid(3);
37+
a.linkNode(b);
38+
a.linkNode(c);
39+
d = (new node()).setid(4);
40+
e = (new node()).setid(5);
41+
f = (new node()).setid(6);
42+
g = (new node()).setid(7);
43+
b.linkNode(d);
44+
b.linkNode(e);
45+
c.linkNode(f);
46+
c.linkNode(g);
47+
(new dfs()).search(a);
48+
}
49+
}

‎other/node.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import java.util.*;
2+
3+
public class node {
4+
private int id = -1;
5+
private static int nextId = 101;
6+
public void init() {
7+
this.id = nextId;
8+
nextId++;
9+
}
10+
ArrayList<node> connectedNodes;
11+
public node() {
12+
init();
13+
this.connectedNodes = new ArrayList<node>(0);
14+
}
15+
public node(node n) {
16+
init();
17+
this.connectedNodes = new ArrayList<node>(1);
18+
this.connectedNodes.add(n);
19+
}
20+
public void connect(node n) {
21+
this.connectedNodes.add(n);
22+
}
23+
public ArrayList<node> getChildNodes(){
24+
return this.connectedNodes;
25+
}
26+
public int graphId;
27+
public void setId(int id) {
28+
this.graphId = id;
29+
}
30+
public node setid(int id) {
31+
this.graphId = id;
32+
return this;
33+
}
34+
public int getId() {
35+
return this.graphId;
36+
}
37+
public int getUniqueId() {
38+
return this.id;
39+
}
40+
/* Diff mode settings
41+
* 0 - compare by id
42+
* 1 - compare by unique id
43+
* 2 - Compare by connected nodes
44+
*/
45+
public static int diffmode = 0;
46+
@Override
47+
public boolean equals(Object obj) {
48+
if(obj instanceof node) {
49+
node e = (node) obj;
50+
if(diffmode == 0) {
51+
return (this.getId() == e.getId());
52+
}else if(diffmode == 1) {
53+
return (this.getUniqueId() == e.getUniqueId());
54+
}else if(diffmode == 2) {
55+
return (this.connectedNodes.equals(e.connectedNodes));
56+
}
57+
}else {
58+
return false;
59+
}
60+
return false;
61+
}
62+
public void linkNode(node n) {
63+
this.connectedNodes.add(n);
64+
}
65+
public node addChild(node n) {
66+
this.linkNode(n);
67+
return this;
68+
}
69+
@Override
70+
public String toString() {
71+
StringBuilder sb = new StringBuilder("node(");
72+
sb.append("id = ");
73+
sb.append(this.graphId);
74+
sb.append(", uniqueId = ");
75+
sb.append(this.getUniqueId());
76+
sb.append(", children = ");
77+
sb.append(this.connectedNodes.toString());
78+
sb.append(" )");
79+
return sb.toString();
80+
}
81+
private boolean visited;
82+
public void reset() {
83+
this.visited = false;
84+
}
85+
public void visit() {
86+
this.visited = true;
87+
}
88+
public boolean isvisited() {
89+
return this.visited;
90+
}
91+
public static void main(String[] args) {
92+
// TODO Auto-generated method stub
93+
node.diffmode = 0;
94+
System.out.println((new node()).addChild(new node()));
95+
}
96+
97+
}

0 commit comments

Comments
(0)

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