Topic 5 Graphs
Topic 5 Graphs
Graphs
Graphs
Graphs vs graphs
https://www.geeksforgeeks.org/graph-data-structure-and-algorithms/
Graphs
Components of a Graph
Vertices:
Vertices are the fundamental units of the graph. Sometimes,
vertices are also known as vertex or nodes. Every
node/vertex can be labeled or unlabelled.
Edges:
Edges are drawn or used to connect two nodes of the graph.
It can be ordered pair of nodes in a directed graph. Edges can
connect any two nodes in any possible way. There are no
rules. Sometimes, edges are also known as arcs. Every edge
can be labeled/unlabelled.
Graphs
https://neo4j.com/blog/graphs-for-artificial-intelligence-and-machine-learning/
Graphs
Types
Labeled
labels (which are most commonly numbers) are assigned to nodes/edges.
Nonlabeled
individual nodes have no distinct identifications except through
their interconnectivity.
Directed
edges have a direction. The edges indicate a one-way relationship, in that
each edge can only be traversed in a single direction.
Undirected (bidirected)
edges do not have a direction. The edges indicate a two-way relationship, in
that each edge can be traversed in both directions.
Graphs
Sequential representation - Adjacency matrix
a square matrix used to represent a finite graph.
https://www.geeksforgeeks.org/add-and-remove-edge-in-adjacency-matrix-representation-
of-a-graph/
Graphs
Linked list representation - Adjacency list
An adjacency list is used in the linked representation to store the
graph
The index of the array represents a node and each element in its
linked list represents the other nodes that form an edge with the
node.
A weighted graph
In the case that the edges have associated numerical information that
represents the cost necessary to traverse that edge, we will say that the
graph is weighted.
Coruña Oviedo 304
Bilbao
171
45
5
32
0
28
296 10
5 Barcelona
19
Valladolid 32
3
9
34
Madrid
3 25
40 1
19 1 Valencia
3 35
Badajoz
241
1 50
Jaén
2
24
99
Sevilla Murcia
256 278
5
12
Cádiz Granada
Graphs
Search
When there are no more nodes left to visit on this path, a step back
(backtracking) is performed, to start the same process with each of
the adjacent nodes of the current node that has already been
processed.
Graphs
BFS Breadth First Search (level order traversal)
The Queue data structure is used for the Breadth First Search traversal.
When we use the BFS algorithm for the traversal in a graph, we can
consider any node as a root node.
Traversing can be started from any node, or we can say that any node
can be considered as a root node until the root node is not mentioned in
the problem.
Graphs
BFS
The element which is deleted from the Queue, the adjacent nodes of
the deleted node are added to the Queue.
A standard BFS implementation puts each node of the graph into one
of two categories:
• Visited
• Not Visited
create a queue Q
mark v as visited and put v into Q
while Q is non-empty
remove the head u of Q
mark and enqueue all (unvisited) neighbours of u
https://www.programiz.com/dsa/graph-bfs
Graphs
BFS
create a queue Q
mark v as visited and put v into Q
while Q is non-empty
remove the head u of Q
mark and enqueue all (unvisited) neighbours of u
https://www.programiz.com/dsa/graph-bfs
Graphs
DFS
The element which is removed from the stack, then only one adjacent
node of a deleted node is added in the stack.
A standard DFS implementation puts each node of the graph into one
of two categories:
• Visited
• Not Visited
DFS(G, u)
u.visited = true
for each v ∈ G.Adj[u]
if v.visited == false
DFS(G,v)
https://www.programiz.com/dsa/graph-dfs
Graphs
DFS
DFS(G, u)
u.visited = true
for each v ∈ G.Adj[u]
if v.visited == false
DFS(G,v)
Stack
https://www.programiz.com/dsa/graph-dfs
Graphs
Example
BFS -> {A B S C G D E F H}
DFS -> {A B S C D E H G F}
Graphs
Dijkstra algorithm - shortest path
https://en.wikipedia.org/wiki/Dijkstras_algorithm
Graphs
Dijkstra algorithm - shortest path
It picks the unvisited vertex with the lowest distance, calculates the distance
through it to each unvisited neighbor, and updates the neighbor's distance
if smaller.
https://en.wikipedia.org/wiki/Dijkstras_algorithm
Graphs
Dijkstra algorithm
https://www.programiz.com/dsa/dijkstra-algorithm
Graphs
Dijkstra algorithm
• (s1,1), (s2,3)
• (s2,3), (s4,5), (s3,9)
• (s4,5), (s3,9)
• (s5,6), (s3,9)
• (s3,7), (t,16)
• (t,14)
struct node
{
int data;
};
Graphs
Examples of basic operations:
struct Graph * CreateGraph(int numNodes);
printGraph (g);
}
Graphs
struct Graph * createGraph (int numNodes)
{
struct Graph * graph=malloc(sizeof(struct Graph));
graph->numNodes=numNodes;
graph->visited=malloc(numNodes*sizeof(int));
…
//1D dynamic matrix
graph->adjMatrix=malloc(numNodes*numNodes*sizeof(int));
//list
void AddEdge(struct Graph * g, int src, int dest)
{
//one direction, new node added at the beginning of the list
struct node * newNode = createNode(dest);
newNode->next = g->adjList[src];
g->adjList[src] = newNode;
//two directions
/*newNode = createNode(src);
newNode->next = g->adjList[dest];
g->adjList[dest] = newNode;*/
}
Graphs
BFS
struct QueueNode struct QueueNode *first;
{
int data; struct QueueNode *last;
struct QueueNode *next;
};
DFS
struct StackNode struct StackNode *top;
{
int data;
struct StackNode *next;
};