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 685bbd4

Browse files
committed
ss
1 parent 719328d commit 685bbd4

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package DataAndAlgoL.Chpt9GraphAlgorithms;
2+
import java.util.*;
3+
4+
import javax.xml.transform.Source;
5+
6+
public class Graph {
7+
/*//specifying a string that represents a new line character
8+
Retrieves the value of the line separator property of the system, which is the character sequence used to separate lines in text files
9+
*/
10+
private static final String NEWLINE= System.getProperty("line.separator");
11+
12+
private final int V; // creates vertex number
13+
private int E; //creates number of edges
14+
private boolean[][] adjMatrix; //creates a matrix of boolean values for graph and dfs algotrithm
15+
boolean[] visited; //creates array to store whether vertices where visited previously
16+
17+
//Empty graph with V vertices
18+
public Graph(int V){
19+
if(V < 0) throw new IllegalArgumentException("Too few vertices"); // checks for error as V cannot be < 0
20+
this.V= V;
21+
this.E=0;
22+
this.adjMatrix= new boolean[V][V]; //creates matrix with number of vertices by number of vertices
23+
visited= new boolean[V]; //creates an array with size V (# of vertices)
24+
}
25+
26+
//random graph with V vertices and E edges
27+
public Graph(int V, int E){
28+
this(V);
29+
if(E > (long) V*(V-1)/2+V) throw new IllegalArgumentException("Too many edges");
30+
if(E<0) throw new IllegalArgumentException("Too few eges");
31+
Random random= new Random();
32+
33+
//can be inefficient
34+
while(this.E != E){
35+
int u= random.nextInt(V);
36+
int v= random.nextInt(V);
37+
addEdge(u,v); // creates edge between adjacent vertices
38+
}
39+
40+
visited= new boolean[V];
41+
}
42+
43+
//number of vertices and edges
44+
public int V(){
45+
return V;
46+
}
47+
48+
public int E(){
49+
return E;
50+
}
51+
52+
//adding undirected edge u-v
53+
public void addEdge(int u, int v){
54+
if(!adjMatrix[u][v]){
55+
E++;
56+
}
57+
adjMatrix[u][v]= true; //undirected edge from u-v
58+
adjMatrix[v][u]=true; //undirected edge from v-u
59+
}
60+
61+
//does graph contain the edge u-v
62+
public boolean contains(int u, int v){
63+
return adjMatrix[u][v];
64+
}
65+
66+
//return list of neighbors of u
67+
public Iterable<Integer> adjMatrix(int u){
68+
return new AdjIterator(u);
69+
}
70+
71+
//support iteration over graph vertices
72+
private class AdjIterator implements Iterator<Integer>, Iterable<Integer>{
73+
private int u;
74+
private int v=0;
75+
76+
AdjIterator(int u){
77+
this.u=u;
78+
}
79+
80+
public Iterator<Integer> iterator() {
81+
return this;
82+
}
83+
84+
@Override
85+
public boolean hasNext() {
86+
while(v < V){
87+
if(adjMatrix[u][v])
88+
return true;
89+
v++;
90+
}
91+
92+
return false;
93+
}
94+
95+
@Override
96+
public Integer next() {
97+
if(!hasNext()){
98+
throw new NoSuchElementException();
99+
}
100+
101+
return v++;
102+
}
103+
104+
public void remove(){
105+
throw new UnsupportedOperationException();
106+
}
107+
108+
}
109+
110+
//String representation of Graph - Takes O(n^2) time
111+
public String toString(){
112+
StringBuilder s = new StringBuilder();
113+
s.append("Undirected graph" + NEWLINE);
114+
s.append("Vertices:"+ V + " and edges:" + E + NEWLINE);
115+
for (int u = 0; u < V; u++) {
116+
s.append(u + ": ");
117+
for (int v = 0; v < V; v++) {
118+
s.append(String.format("%7s", adjMatrix[v][u]) + " ");
119+
}
120+
s.append(NEWLINE);
121+
}
122+
return s.toString();
123+
}
124+
125+
void clearVisited(){
126+
for (int i = 0; i < visited.length; i++)
127+
visited[i] = false;
128+
}
129+
130+
//ALGORITHM IMPORTANT PART
131+
public void dfs(){
132+
//visit nodes using a stack to store "to visit" nodes
133+
Stack<Integer> s= new Stack<>();
134+
clearVisited(); //set all visited[i]=0;
135+
s.push(0); // start the "to visit" at node 0
136+
137+
//LOOP AS LONG AS THERE ARE ACTIVE NODES
138+
while(!s.isEmpty()){
139+
int nextNode; //next Node to visit
140+
int i;
141+
nextNode= s.pop();
142+
if(!visited[nextNode]){ //current node has't been visited
143+
visited[nextNode]=true; //mark node as visited
144+
System.out.println("nextNode "+nextNode);
145+
for(i=0 ; i< V; i++){ //for i < than the total number of vertices
146+
if((this.adjMatrix[nextNode][i]==true) && !this.visited[i]){ // if next column assigning to the next node from current node is true in matrix and has not beed visited, push that next node
147+
s.push(i); //node pushed that has a connection of value true to previous node
148+
}
149+
}
150+
}
151+
}
152+
}
153+
154+
public static void main(String[] args) {
155+
int V =10;
156+
int E =13;
157+
Graph G= new Graph(V,E);
158+
System.out.println(G.toString());
159+
G.dfs();
160+
}
161+
}

‎LeetCode/Blind75/Lt200.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package Blind75;
2+
/*Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
3+
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
4+
*/
5+
6+
/*Example:
7+
* Input: grid = [
8+
["1","1","1","1","0"],
9+
["1","1","0","1","0"],
10+
["1","1","0","0","0"],
11+
["0","0","0","0","0"]
12+
]
13+
Output: 1
14+
*/
15+
public class Lt200 {
16+
public static void main(String[] args) {
17+
18+
}
19+
20+
public int numIslands(char[][] grid) {
21+
int count = 0;
22+
//goes throght entire matrix
23+
for (int i = 0; i < grid.length; i++) {
24+
for (int j = 0; j < grid[i].length; j++) {
25+
if (grid[i][j] == '1') { //if cell at position i,j is character 1
26+
count++; //counter of islands increase
27+
clearRestOfLand(grid, i, j); //call clear rest of land method which is suppose to clear chars "1" to "0" and continue exploring matrix
28+
}
29+
}
30+
}
31+
return count;
32+
}
33+
34+
//this method is meant to only clear the cells of the islands from 1 to 0 so when backtracking we dont count those cells again
35+
private void clearRestOfLand(char[][] grid, int i, int j) {
36+
if (i < 0 || j < 0 || i >= grid.length || j >= grid[i].length || grid[i][j] == '0') return; //for out of bounds in the matrix or i,j have value 0 return nothing
37+
38+
grid[i][j] = '0'; // position at i,j where method was called in the above method turn it from 1 to 0
39+
//Clear the rest of the island in case more than the i,j cell at left, right, bottom, top have also the value of 1
40+
clearRestOfLand(grid, i+1, j);
41+
clearRestOfLand(grid, i-1, j);
42+
clearRestOfLand(grid, i, j+1);
43+
clearRestOfLand(grid, i, j-1);
44+
return;
45+
}
46+
}

0 commit comments

Comments
(0)

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