-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
81 lines (72 loc) · 1.9 KB
/
Graph.java
File metadata and controls
81 lines (72 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// A Java program to implement graph coloring
import java.io.*;
import java.util.*;
import java.util.LinkedList;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
// This class represents an undirected graph using adjacency list
class Graph
{
private int V; // No. of vertices
private LinkedList<Integer> adj[]; //Adjacency List
//Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
//Function to add an edge into the graph
void addEdge(int v,int w)
{
adj[v].add(w);
adj[w].add(v); //Graph is undirected
}
public int getV()
{
return V;
}
public void printGraph()
{
for(int i=0; i<V; i++)
{
System.out.print(i + ": ");
Iterator<Integer> itr=adj[i].iterator();
while(itr.hasNext()){
System.out.print(itr.next() + " ");
}
System.out.print(" - Size: " + adj[i].size() + " ");
System.out.println();
}
}
public LinkedList<Integer> getListAdj(int u)
{
return adj[u];
}
public boolean readGraph(String input_name, int result[])
{
try{
Scanner fileReader = new Scanner(new File(input_name));
int v = 0;
while(fileReader.hasNextInt()){
result[v] = fileReader.nextInt();
v++;
}
for(int i = 0; i < result.length; i++){
LinkedList<Integer> newConect = new LinkedList<>();
for(int j = i+1; j < result.length; j++){
if((i%9 == j%9 || i/9 == j/9 ||
i/9/3*3 + i%9/3 == j/9/3*3 + j%9/3) &&
i != j){
addEdge(i,j);
}
}
}
}
catch(Exception e){System.out.println(e);};
return true;
}
}
// This code is contributed by Anh Vo