Skip to content

Commit 003890b

Browse files
committed
Initial commit: release v1.0.0
0 parents  commit 003890b

File tree

11 files changed

+883
-0
lines changed

11 files changed

+883
-0
lines changed

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# IntelliJ IDEA specific files
2+
.idea/
3+
*.iws
4+
*.iml
5+
*.ipr
6+
7+
# Compiled class file
8+
*.class
9+
10+
# Log file
11+
*.log
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*
24+
25+
# Maven specific files
26+
target/
27+
pom.xml
28+
pom.xml.tag
29+
pom.xml.releaseBackup
30+
pom.xml.versionsBackup
31+
pom.xml.next
32+
release.properties
33+
34+
# Eclipse specific files
35+
.metadata
36+
bin/
37+
tmp/
38+
39+
# Other files
40+
.DS_Store
41+
*.swp
42+
*.swo

Graph Concepts.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Graph Concepts for future reference
2+
3+
A. Graph Representations: Adjacency Matrix, Adjacency List, Incidence Matrix
4+
5+
B. Types of Graphs: Directed and Undirected Graphs, Weighted and Unweighted Graphs, Cyclic and Acyclic Graphs, Connected and Disconnected Graphs, Bipartite Graphs, Complete Graphs
6+
7+
C. Graph Algorithms
8+
1. Traversal Algorithms: DFS, BFS
9+
2. Shortest Path Algorithms: Dijkstra's, Bellman-Ford, Floyd-Warshall
10+
3. Minimum Spanning Tree (MST) Algorithms: Prim's Algorithm, Kruskal's Algorithm
11+
4. Topological Sorting
12+
5. Strongly Connected Components (SCC)
13+
6. Graph Coloring
14+
7. Network Flow Algorithms: Ford-Fulkerson Algorithm, Edmonds-Karp Algorithm
15+
8. Cycle Detection: Union-Find Algorithm, DFS
16+
9. Eulerian and Hamiltonian Paths/Cycles
17+
10. Graph Matching: Maximum Bipartite Matching, Hungarian Algorithm
18+
19+
D. Advanced Topics
20+
1. Planar Graphs
21+
2. Graph Isomorphism
22+
3. Dynamic Graph Algorithms
23+
4. Graph Databases
24+
25+
E. Practical Applications
26+
1. Social Networks
27+
2. Computer Networks
28+
3. Geographical Information Systems (GIS)
29+
4. Scheduling Problems
30+
5. Recommendation Systems

readme.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Graph Algorithms Library
2+
3+
Welcome to the Graph Algorithms Library! 🎉
4+
5+
This Java utility library for Graph data structure contains support for various types of graphs viz. Directed/Undirected, Weighted/Unweighted Graphs, Cyclic/Acyclic Graphs, Connected/Disconnected Graphs, Bipartite Graphs, Complete Graphs etc. With multiple algorithms to use with the Graph data structure, independent use cases of the nodes & edges is also possible for custom algorithms and projects.
6+
7+
## Algorithms
8+
9+
- **Breadth-First Search (BFS)**
10+
- **Depth-First Search (DFS)**
11+
- **Dijkstra's Algorithm**
12+
13+
## Contributing
14+
15+
With a dream of adding this library in the official Java Collections Framework, I am keeping this project open to contributions ☁️✨. Got an idea for an awesome new feature? Found a bug that's been bugging you? Want to add some witty comments of your own? We'd love to hear from you!
16+
17+
1. Fork the repository.
18+
2. Create a new branch (git checkout -b feature-branch).
19+
3. Make your changes.
20+
4. Commit your changes (git commit -am 'Add new feature').
21+
5. Push to the branch (git push origin feature-branch).
22+
6. Create a new Pull Request.
23+
24+
## Contact
25+
If you have any questions, suggestions, or just want to say hi, feel free to reach out:
26+
27+
- Email: kushalv238@gmail.com
28+
- LinkedIn: [kushal-vadodaria](https://www.linkedin.com/in/kushal-vadodaria/)
29+
30+
***
31+
32+
Remember, life is just like a graph: complex, intertwined and confusing but there is always a way around. Sometimes you just need to take a step back (like a breadth-first search) to see the bigger picture, but other times you need to dive deep (like a depth-first search) to uncover the hidden treasures. Happy Graphing! 🚀
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Represents a directed graph.
8+
*
9+
* @param <T> the type of the node data
10+
*/
11+
public class DirectedGraph<T> implements Graph<T> {
12+
private final List<Node<T>> nodes;
13+
14+
public DirectedGraph() {
15+
this.nodes = new ArrayList<>();
16+
}
17+
18+
@Override
19+
public List<Node<T>> getNodes() {
20+
return new ArrayList<>(nodes); // Return a copy to prevent external modification
21+
}
22+
23+
@Override
24+
public Node<T> addNode(T data) {
25+
if (data == null) {
26+
throw new IllegalArgumentException("Node data cannot be null");
27+
}
28+
29+
Node<T> node = new Node<>(data);
30+
nodes.add(node);
31+
return node;
32+
}
33+
34+
@Override
35+
public Node<T> addNode(T data, String name) {
36+
Node<T> node = new Node<>(data, name);
37+
nodes.add(node);
38+
return node;
39+
}
40+
41+
@Override
42+
public void connect(Node<T> fromNode, Node<T> toNode, double weight) {
43+
validateNodesInGraph(fromNode, toNode);
44+
fromNode.connect(toNode, weight);
45+
}
46+
47+
@Override
48+
public void disconnect(Node<T> fromNode, Node<T> toNode) {
49+
validateNodesInGraph(fromNode, toNode);
50+
fromNode.disconnect(toNode);
51+
}
52+
53+
@Override
54+
public void updateNodeData(Node<T> node, T newData) {
55+
if (node == null || newData == null) {
56+
throw new IllegalArgumentException("Node and new data cannot be null");
57+
}
58+
59+
if (!nodes.contains(node)) {
60+
throw new IllegalArgumentException("Node must be part of the graph");
61+
}
62+
63+
node.setData(newData);
64+
}
65+
66+
@Override
67+
public void updateEdgeWeight(Node<T> fromNode, Node<T> toNode, double newWeight) {
68+
validateNodesInGraph(fromNode, toNode);
69+
70+
for (Edge<T> edge : fromNode.getEdges()) {
71+
if (edge.getToNode().equals(toNode)) {
72+
edge.setWeight(newWeight);
73+
return;
74+
}
75+
}
76+
77+
throw new IllegalArgumentException("Edge does not exist");
78+
}
79+
80+
/**
81+
* Validates that both nodes are part of the graph.
82+
*
83+
* @param fromNode the starting node
84+
* @param toNode the ending node
85+
* @throws IllegalArgumentException if either node is not part of the graph
86+
*/
87+
private void validateNodesInGraph(Node<T> fromNode, Node<T> toNode) {
88+
if (fromNode == null || toNode == null) {
89+
throw new IllegalArgumentException("Nodes cannot be null");
90+
}
91+
92+
if (!nodes.contains(fromNode) || !nodes.contains(toNode)) {
93+
throw new IllegalArgumentException("Both nodes must be part of the graph");
94+
}
95+
}
96+
}

src/main/java/graph/Edge.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package graph;
2+
3+
/**
4+
* Represents an edge in a graph connecting two nodes with a weight.
5+
*
6+
* @param <T> the type of the node data
7+
*/
8+
public class Edge<T> {
9+
private final Node<T> fromNode;
10+
private final Node<T> toNode;
11+
private double weight;
12+
13+
/**
14+
* Constructs an edge with a specified weight.
15+
*
16+
* @param fromNode the starting node
17+
* @param toNode the ending node
18+
* @param weight the weight of the edge
19+
* @throws IllegalArgumentException if fromNode or toNode is null
20+
*/
21+
Edge(Node<T> fromNode, Node<T> toNode, double weight) {
22+
if (fromNode == null || toNode == null) {
23+
throw new IllegalArgumentException("fromNode and toNode cannot be null");
24+
}
25+
26+
this.fromNode = fromNode;
27+
this.toNode = toNode;
28+
this.weight = weight;
29+
}
30+
31+
/**
32+
* Constructs an edge with a default weight of 1.0.
33+
*
34+
* @param fromNode the starting node
35+
* @param toNode the ending node
36+
*/
37+
Edge(Node<T> fromNode, Node<T> toNode) {
38+
this(fromNode, toNode, 1.0);
39+
}
40+
41+
public double getWeight() {
42+
return weight;
43+
}
44+
45+
public void setWeight(double weight) {
46+
this.weight = weight;
47+
}
48+
49+
public Node<T> getFromNode() {
50+
return fromNode;
51+
}
52+
53+
public Node<T> getToNode() {
54+
return toNode;
55+
}
56+
}

src/main/java/graph/Graph.java

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package graph;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Represents a generic graph.
7+
*
8+
* @param <T> the type of the node data
9+
*/
10+
public interface Graph<T> {
11+
12+
List<Node<T>> getNodes();
13+
14+
/**
15+
* Adds a new node with the specified data to the graph.
16+
*
17+
* @param data the data of the new node
18+
* @return the newly added node
19+
*/
20+
Node<T> addNode(T data);
21+
22+
/**
23+
* Adds a new node with the specified data and name to the graph.
24+
*
25+
* @param data the data of the new node
26+
* @param name the name of the new node
27+
* @return the newly added node
28+
*/
29+
Node<T> addNode(T data, String name);
30+
31+
/**
32+
* Connects two nodes in the graph with a specified weight.
33+
*
34+
* @param fromNode the starting node
35+
* @param toNode the ending node
36+
* @param weight the weight of the connection
37+
* @throws IllegalArgumentException if either node is not part of the graph
38+
*/
39+
void connect(Node<T> fromNode, Node<T> toNode, double weight);
40+
41+
/**
42+
* Connects two nodes in the graph with a default weight of 1.0.
43+
*
44+
* @param fromNode the starting node
45+
* @param toNode the ending node
46+
*/
47+
default void connect(Node<T> fromNode, Node<T> toNode) {
48+
this.connect(fromNode, toNode, 1.0);
49+
}
50+
51+
/**
52+
* Connects a node to multiple nodes with specified weights.
53+
*
54+
* @param fromNode the starting node
55+
* @param toNodes the list of ending nodes
56+
* @param weights the list of weights
57+
* @throws IllegalArgumentException if fromNode is null, or if toNodes or weights are null or different sizes
58+
*/
59+
default void connect(Node<T> fromNode, List<Node<T>> toNodes, List<Double> weights) {
60+
if (fromNode == null || toNodes == null || weights == null || toNodes.size() != weights.size()) {
61+
throw new IllegalArgumentException("Invalid nodes or weights");
62+
}
63+
for (int i = 0; i < toNodes.size(); i++) {
64+
this.connect(fromNode, toNodes.get(i), weights.get(i));
65+
}
66+
}
67+
68+
/**
69+
* Connects a node to multiple nodes with a default weight of 1.0.
70+
*
71+
* @param fromNode the starting node
72+
* @param toNodes the list of ending nodes
73+
* @throws IllegalArgumentException if fromNode or toNodes is null
74+
*/
75+
default void connect(Node<T> fromNode, List<Node<T>> toNodes) {
76+
if (fromNode == null || toNodes == null) {
77+
throw new IllegalArgumentException("Invalid nodes");
78+
}
79+
for (Node<T> toNode : toNodes) {
80+
this.connect(fromNode, toNode, 1.0);
81+
}
82+
}
83+
84+
/**
85+
* Disconnects two nodes in the graph.
86+
*
87+
* @param fromNode the starting node
88+
* @param toNode the ending node
89+
* @throws IllegalArgumentException if fromNode or toNode is null
90+
*/
91+
void disconnect(Node<T> fromNode, Node<T> toNode);
92+
93+
/**
94+
* Disconnects a node from multiple nodes.
95+
*
96+
* @param fromNode the starting node
97+
* @param toNodes the list of ending nodes
98+
* @throws IllegalArgumentException if fromNode or toNodes is null
99+
*/
100+
default void disconnect(Node<T> fromNode, List<Node<T>> toNodes) {
101+
if (fromNode == null || toNodes == null) {
102+
throw new IllegalArgumentException("Invalid nodes");
103+
}
104+
for (Node<T> toNode : toNodes) {
105+
this.disconnect(fromNode, toNode);
106+
}
107+
}
108+
109+
/**
110+
* Updates data of a node
111+
*
112+
* @param node the node whose data needs to be updated
113+
* @param newData the updated new data
114+
* @throws IllegalArgumentException if node or newData is null or node is not part of this graph
115+
*/
116+
void updateNodeData(Node<T> node, T newData);
117+
118+
/**
119+
* Updates weight of an edge
120+
*
121+
* @param fromNode the starting node of the edge
122+
* @param toNode the ending node of the edge
123+
* @param newWeight the updated new weight
124+
* @throws IllegalArgumentException if either start and end nodes are not part of the graph or the edge doesn't exist between the two nodes
125+
*/
126+
void updateEdgeWeight(Node<T> fromNode, Node<T> toNode, double newWeight);
127+
}

0 commit comments

Comments
 (0)