example graph:
see examples/06_GRAPH_BASED_DSA/_images/basic-undirected-graph
// Graph adjacency list representation
| V |
| 0 | -> 1 -> 2 -> 3
| 1 | -> 0 -> 2
| 2 | -> 0 -> 1
| 3 | -> 0
see examples/06_GRAPH_BASED_DSA/_images/adjacency-list/adjacency-list_graph-representation
asides:
- stay close to basic definition of graph: collection of vertices and edges
- use unlabeled graph (i.e. vertices identified by their indices) as opposed to a labeled one
{/* TODO: specifying C++ for the block seems to break syntax highlighting for rest of file? */}
struct node {
int vertex;
struct node* next;
}
struct Graph {
int numVertices;
struct node** adjLists;
}
see site for explanation of struct node** adjLists
class Graph {
int numVertices;
list<int> *adjLists;
public:
Graph(int V);
void addEdge(int src, int dest);
};
class Graph {
private int numVertices;
private LinkedList<integer> adjLists[];
}
graph = {
'A': set(['B', 'C']),
'B': set(['A', 'D', 'E']),
'C': set(['A', 'F']),
'D': set(['B']),
'E': set(['B', 'F']),
'F': set(['C', 'E'])
}
NOTE: THESE ARE INCOMPLETE
Python
class AdjNode:
def __init__(self, value):
self.vertex = value
self.next = None
class Graph:
def __init__(self, num):
self.V = num
self.graph = [None] * self.V
# Add edges
def add_edge(self, src, dest):
node = AdjNode(dest)
node.next = self.graph[src]
self.graph[src] = node
node = AdjNode(src)
node.next = self.graph[dest]
self.graph[dest] = node
# Print the graph
def print_graph(self):
for i in range(self.V):
print("Vertex " + str(i) + ":", end="")
temp = self.graph[i]
while temp:
print(" -> {}".format(temp.vertex), end="")
temp = temp.next
print(" \n")
if __name__ == "__main__":
V = 5
# Create graph and edges
graph = Graph(V)
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(0, 3)
graph.add_edge(1, 2)
graph.print_graph()
C++
#include <bits/stdc++.h>
using namespace std;
// Add edge
void addEdge(vector<int> adj[], int s, int d) {
adj[s].push_back(d);
adj[d].push_back(s);
}
// Print the graph
void printGraph(vector<int> adj[], int V) {
for (int d = 0; d < V; ++d) {
cout << "\n Vertex "
<< d << ":";
for (auto x : adj[d])
cout << "-> " << x;
printf("\n");
}
}
int main() {
int V = 5;
// Create a graph
vector<int> adj[V];
// Add edges
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 0, 3);
addEdge(adj, 1, 2);
printGraph(adj, V);
}
Java
import java.util.*;
class Graph {
private int numVertices;
private LinkedList<integer> adjLists[];
public static void main(String[] args) {
int V = 5;
ArrayList<ArrayList<Integer>> am = new ArrayList<ArrayList<Integer>>(V);
for (int i = 0; i < V; i++) {
am.add(new ArrayList<Integer>());
}
addEdge(am, 0, 1);
addEdge(am, 0, 2);
addEdge(am, 0, 3);
addEdge(am, 1, 2);
printGraph(am);
}
// seems like this should use LinkedLists?
static void addEdge(
ArrayList<ArrayList<Integer>> adjMatrix,
int src,
int dest
) {
am.get(s).add(d);
am.get(d).add(s);
}
static void printGraph(ArrayList<ArrayList<Integer>> am) {
for (int i = 0; i < am.size(); i++) {
System.out.println("\nVertex " + i + ":");
for (int j = 0; j < am.get(i).size(); j++) {
System.out.print(" -> " + am.get(i).get(j));
}
System.out.println();
}
}
}