Graph Report
Graph Report
Graph Report
What is a Graph?
A graph is a Non-Linear Data Structure which consists of set of nodes called vertices V and
set of edges E which links vertices. The set of edges describes relationships among the
vertices.
Formal definition of Graph
A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
Directed vs. undirected Graph
When the edges in a graph have no direction, the graph is called undirected
UNDIRECTED GRAPH
When the edges in a graph have a direction, the graph is called directed (or digraph). If
the graph is directed, the order of the vertices in each edge is important.
DIRECTED GRAPH
GRAPH TERMINOLOGY
Adjacent nodes: Two nodes are adjacent if they are connected by an edge.
5 is adjacent to 7
7 is adjacent from 5
GRAPH IMPLEMENTATION
Array-based implementation
A 1D array is used to represent the vertices.
A 2D array (adjacency matrix) is used to represent the edges.
Linked-list implementation
A 1D array is used to represent the vertices.
A list is used for each vertex v which contains the vertices which are adjacent
from v (adjacency list).
GRAPH SEARCHING
Methods: Depth-First-Search (DFS) or Breadth-First-Search (BFS).
Depth-First-Search (DFS)
What is the idea behind DFS?
Travel as far as you can down a path
4
Back up as little as possible when you reach a "dead end" (i.e., next vertex
has been "marked" or there is no next vertex).
DFS can be implemented efficiently using a stack.
Breadth-First-Searching (BFS)
What is the idea behind BFS?
Look at all possible paths at the same depth before you go at a deeper level.
Back up as far as possible when you reach a "dead end" (i.e., next vertex has
been "marked" or there is no next vertex).
BFS can be implemented efficiently using a queue.
}
private void printPath() {
if (this == this.previous) {
System.out.printf("%s",
this.name);
private void algo(final
NavigableSet<Vertex> q) {
Vertex u, v;
while (!q.isEmpty()) {
u = q.pollFirst(); // vertex with
shortest distance (first iteration will
return source)
if (u.dist ==
Integer.MAX_VALUE) break; // we
can ignore u (and any other remaining
vertices) since they are unreachable
//look at distances to each
neighbour
for (Map.Entry<Vertex, Integer>
a : u.neighbours.entrySet()) {
v = a.getKey(); //the neighbour
in this iteration
final int alternateDist = u.dist +
a.getValue();
if (alternateDist < v.dist) { //
shorter path to neighbour found
q.remove(v);
v.dist = alternateDist;
v.previous = u;
q.add(v);
}
}
}
}
/** Prints a path from the source to
the specified vertex */
public void printPath(String
endName) {
if (!graph.containsKey(endName))
{
System.err.printf("Graph doesn't
contain end vertex \"%s\"\n",
endName);
return;
}
graph.get(endName).printPath();
System.out.println();
}
/** Prints the path from the source to
every vertex (output order is not
guaranteed) */
public void printAllPaths() {
for (Vertex v : graph.values()) {
v.printPath();
System.out.println();
} }}
Output: