0% found this document useful (0 votes)
12 views43 pages

Dsa24 8

The document discusses graphs and graph algorithms. It covers graph terminology including vertices, edges, paths, cycles, connectedness and graph representations using adjacency matrices and adjacency lists. It also describes basic graph operations and graph traversal algorithms including depth-first search and breadth-first search.

Uploaded by

parmarnupur44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views43 pages

Dsa24 8

The document discusses graphs and graph algorithms. It covers graph terminology including vertices, edges, paths, cycles, connectedness and graph representations using adjacency matrices and adjacency lists. It also describes basic graph operations and graph traversal algorithms including depth-first search and breadth-first search.

Uploaded by

parmarnupur44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Lecture 8

Graphs
Main content of last lecture
§ Basic tree concepts: general tree Hard to implement

§ Binary tree Extension of linked list

§ Convert a general tree into a binary tree


§ DFT and 3 commonly used DF traversal strategies
for binary trees
Preorder, inorder and postorder

§ Binary Search Tree More than sorted

§ Complexity of operations in binary search trees


There is a potential O(log n) but no guarantee
3

Major content of this lecture


§ Learn about graphs
§ Terminology of graph theory
§ Representation of graphs
§ Examine and implement various graph traversal
algorithms
§ Minimum spanning tree algorithms
§ Shortest path algorithms
list
Introduction
§ A graph consists of a finite set of nodes called vertices,
denoted by V, and a finite set of line segments called lines
connecting pairs of vertices, denoted by E. Thus a graph
can be expressed as
G = (V, E)
e1
v1 v2
e3 Vertices hold data
e7
e2 e4 v4 and edges specify
e6 relationships
among data.
v6 e5 v3
v5
5

Example of graphs
6

Graphs that are not “graphs” in DSA


Introduction
§ A graph can be directed or undirected (non-directed).
§ Lines in a directed graph (digraph) are directed and are
called arcs. The direction indicates how the arc can be
traversed.
§ Lines in an undirected graph are undirected and are
called edges. The edges can be traversed in either
direction
Directed graphs and undirected graphs
Basic concepts ✘ ✘

§ Path: a sequence of vertices in which each vertex is adjacent to the


next one so that you can travel from one end to another end via each
vertex.
§ Cycle: a path that starts and ends at the same vertex
§ Connected (for undirected graphs): any two vertices are connected
by a path.
§ Connected (for directed graphs):
§ Strongly connected: every vertex can reach any vertex and can
be reached from any vertex
§ Weakly connected: the associated undirected graph is connected
(by suppressing directions).
§ Disjoint: a graph is not connected
§ Tree: connected graph with no cycles
Connected and disjoint graphs

Connected and disjoint


graphs
Basic concepts, continued
§ Degree: the number of lines incident to a vertex
§ outdegree: the number of arcs leaving a vertex
Directed
§ indegree: the number of arcs entering a vertex graphs
§ Weighted lines (arcs or edges): each arc or edge is associated
with a number (label).
§ Network (weighted graph): a graph whose lines are weighted
13

Graph representation (mathematically)

Directed graphs

V(G) = {1,2,3,4,5}
E(G)={(1,2), (2,1), (1,5), (5,1), (5,2), (2,5), (2,3), (3,2),
(2,4), (4,2), (3,4), (4,3), (4,5), (5,4)}

Like any binary search tree, each note can


store any data. The number of node just
undirected graph indicates the key of the node.
Graph implementation
§ Two common ways of implementing graphs (directed
and undirected) are: adjacency matrix and adjacency lists
§ Adjacency matrix
§ use an array/list/vector/map to store the vertices and
§ use a matrix to store the lines or arcs: an n⨉n matrix
Adj(i, j) such that:

1, if (i, j) Î E

Adj(i, j) =
0, otherwise
Adjacency matrix
Typically contain data with with key

Adjacency

matrix
16

Adjacency matrix for weighted graph

§ Weighted graph (network) Weights in adjacency matrix


vertices 0 1 2 3 4 5 6
0 * 0 0 6 5 2 0 0 0
1 * 1 6 0 0 0 2 0 4
2 * 2 5 0 0 0 0 7 5
3 * 3 2 0 0 0 8 0 0
4 * 4 0 2 0 8 0 10 0
5 * 5 0 0 7 0 10 0 0
6 * 6 0 4 5 0 0 0 0

Data associated with vertices Weights associated with edges


All weights must be non-
zero. You can normalize DBL_MAX Zero ó no line in between
weights in your way. INT_MAX Symmetry ó undirected
Adjacency Lists
Data with Key Key only

Hold data

Adjacency list:

• Main linked list stores all the vertices


• Each associated linked-list stores the keys of adjacent vertices
Basic Operations on Graphs
§ Insertion:
§ Add a vertex: add a new node to an existing graph with
specified lines
§ Add a line/arc: connect a vertex to another vertex (must
indicate two vertices, for digraph must specify source and
destination)
§ Deletion:
§ Delete a vertex: remove a vertex together with all
connecting lines from the vertex
§ Delete a line/arc: remove a line between two vertices.
Basic Operations on Graphs
§ Search: search for a node that contains a specific key
or data item. Can use different search strategies, such
as depth-first, breath-first or best-first search.
§ Traversal: visit all vertices in a graph, typically using
§ depth-first traversal: go as far as possible; trace
back once reach a dead end
§ breadth-first traversal: visit all the vertices in one
level before move to the next level.
Level: The length of the shortest path from the source vertex.
20

Depth First Traversal


DFT_Algorithm No loop is needed if the
For each vertex graph is connected The algorithm is slightly
different from game tree
Push the vertex into a stack if it has not been visited
search because a tree
While the stack is not empty is always connected.
Pop a vertex from the stack and mark it as visited
Process the data on the vertex
For each adjacent vertex of the popped vertex The outcomes of
traversal are not unique,
Push it into the stack if it has not been visited
depending on the
adjacency list.

auxiliary data
structures:
list and stack
21

Depth First Traversal: recursion algorithm


for (int v = 0; v < gSize; v++) list of visited
if (!visited[v]) vertices Adjacent list for
depth_first_traversal(v, visited); vertex v (’s index)
depth_first_traversal(int v, bool visited[]) {
visited[v] = true;
cout << v << " is visited” << endl; //visit the vertex
// add code for visiting the node
for (it = graph[v].begin(); it != graph[v].end();++it) {
if (!visited[*it])
depth_first_traversal(*it, visited);
} Graphs are represented in
} adjacency lists.

Stacks are used implicitly in


The function can the algorithm since recursive
cover all nodes in a function calls use stacks.
connected piece of
the graph
22
Data structures needed: one
Breadth First Traversal queue and one array of
Boolean
BFT_Algorithm
For each vertex
Push the vertex into a queue if it has not been visited
While the queue is not empty
Pop a vertex from the queue and mark it as visited
Process the data on vertex
For each adjacent vertex of the popped vertex
Push it into the queue if it has not been visited

auxiliary data
structures:
list and queue
23

Breath First Traversal: queue


void graphType::depthFirstTraversal() {
bool visited[gSize];
queue<int> bfQueue;
for (int i = 0; i < gSize; i++) { //for connected graph, it is not needed
if (!visited[i]) {
bfQueue.push(i);
visited[index] = true;
while (!bfQueue.empty()) {
int k = bfQueue.front();
bfQueue.pop();
cout << k << ” is visited ” << endl; //visit the vertex
for (graphIt = graph[k].begin();graphIt != graph[k].end(); ++graphIt) {
int w = *graphIt;
if (!visited[w])
bfQueue.push(w); • Vertices are represented by
} // end of for integer from 0 to gSize-1.
} // end of while
• Adjacency linked-list is
} // end of if
} // end of for stored in graph[i] for each
} vertex i.
24

Implementation of DFT and BFT


0
Depth First Traversal:
1 0 1 2 4 3 5 6 8 10 7 9
2 5
Breadth First Traversal:
4 6
0 1 5 2 3 6 4 8 10 7 9
Result is not unique.
3
8
Since all vertices are represented in a vertex list,
7 we can always use the list to traverse the graph.
DFT and BFT matter only with respect to the
10
order of visiting, which is extremely important to
9 a graph (tracing coronavirus infection, spanning
trees).
This is not a template ADT. It stores
Read code: graphType.h integers only. This is ok because we
store data in the vertices.
25

Major content of this lecture


§ Learn about graphs
§ Terminology of graph theory
§ Representation of graphs
§ Examine and implement various graph traversal
algorithms
§ Minimum spanning tree algorithms
§ Shortest path algorithms
26

Minimum Spanning Tree


§ City map Other applications:
• Train
• Flight
• Internet
• Power line
• Telephone line
• Gas line
• …

What is the most efficient connection that


can cover all the cities?
Spanning Tree and Minimum Spanning Tree

§ Finding a spanning tree in a graph is a classic problem in


graph theory.
§ A spanning tree of a connected undirected graph (or a
strongly connected digraph) G=(V, E) is a tree containing
all the vertices of G.
§ For a weighted graph (network), a minimum spanning
tree of the graph is a spanning tree with minimal total
weights.

a y h a v e m u ltiple
A g ra p h m m u ltiple
e e s a n d
spanning tr tr ees
m s p a n n in g
minimu
Spanning Tree in a graph

Approaches we can use to generate a spanning tree:


• Find any circle and drop an edge from the circle.
• Depth first traversal
• Breadth first traversal tematic
on-sys
tic vs n
tema
Sys
Minimum Spanning Tree…
§ A minimum spanning tree of a weighted graph G is a
spanning tree of G in which the sum of weights is a
minimum
§ Two best known algorithms are
§ Prim’s Algorithm
§ Kruskal’s Algorithm (not covered here)
§ Prim’s algorithm (greedy algorithm):
§ starts with a vertex v and grows the rest of the tree by
adding one edge at a time
§ always select the edge with the smallest weight to add
to the tree
Minimum Spanning Tree algorithm
Minimum spanning tree algorithm
Select a vertex v to start the tree
While there are still vertices not included in the tree
• Select an edge with the minimum weight from all edges connecting a
tree vertex and a non-tree vertex
• Add the selected edge to the tree

Source Vertex: A
Edges Weight
(B, C) 2
(C, A) 3
(D, C) 3
(E, D) 2
(F, D) 3
31

Prim’s Algorithm for MST


void minimumSpanning(int sVertex) {
int startVertex, endVertex; double minWeight;
source = sVertex;
//record the spanning tree
int *edges = new int[size];
//record minimal weights
double *edgeWeights = new double[size];
//record the progress of tree generation
bool *mstv = new bool[gSize];

//Initialisation
for (int j = 0; j < gSize; j++) {
mstv[j] = false;
edges[j] = source;
edgeWeights[j] = weights[source][j];
}
32

Prim’s Algorithm for MST


mstv[source] = true;
edgeWeights[source] = 0;

for (int i = 0; i < gSize - 1; i++) {


minWeight = DBL_MAX;
for (int j = 0; j < gSize; j++)
if (mstv[j])
for (int k = 0; k < gSize; k++)
if (!mstv[k] && weights[j][k] < minWeight){
endVertex = k;
startVertex = j;
minWeight = weights[j][k];
}
mstv[endVertex] = true;
edges[endVertex] = startVertex;
edgeWeights[endVertex] = minWeight;
} //end for
} //end minimumSpanning
33

Major content of this lecture

§ Learn about graphs


§ Terminology of graph theory
§ Representation of graphs
§ Examine and implement various graph traversal
algorithms
§ Minimum spanning tree algorithms
§ Shortest path algorithms
34

Shortest paths
§ Applications of finding shortest paths
§ transport,
§ navigation
§ communication
§ image segmentation
§ speech processing
§ graph drawing
§ …
Shortest path in a graph
§ Two types of shortest path problems:
§ Single source to all destinations
§ The shortest path between any pair of locations
§ The most famous algorithm for single source to all
destinations is known as Dijkstra’s algorithm ( O(n2) )
§ Applies to both directed and undirected graphs

Edsger W. Dijkstra – the father of DSA


Received Turning Award in 1972.
Shortest Path: Dijkstra’s algorithm
§ Find the shortest paths from a given vertex to all
other vertices by building a spanning tree:
§ Start with tree containing only the starting vertex
§ For each vertex that is not in the tree but adjacent to a
vertex of the tree, mark the total path weight (i.e.,
distance) from the tree root to this vertex on each edge
that link the vertex. Select the vertex and edge with the
minimum distance and insert them into the tree.
§ Repeat the above step until all vertices are in the tree
Shortest Path…

Determine
shortest paths
Shortest Path…

Determine
shortest path
39

Dijkastra’s Shortest Path Algorithm


void shortestPath(int vertex) {
for (int j = 0; j < gSize; j++)
smallestWeight[j] = weights[vertex][j]; //may be DBL_MAX
bool *weightFound = new bool[gSize]; //Record existing spanning tree
for (int j = 0; j < gSize; j++) weightFound[j] = false;
weightFound[vertex] = true;
smallestWeight[vertex] = 0;

for (int i = 0; i < gSize - 1; i++) { A vertex may have


double minWeight = DBL_MAX; a shorter path
int v; through a previous
for (int j = 0; j < gSize; j++) added vertex
if (!weightFound[j])
if (smallestWeight[j] < minWeight) {
v = j;
minWeight = smallestWeight[v];
}
weightFound[v] = true; //Add the vertex into the spanning tree
for (int j = 0; j < gSize; j++)
if (!weightFound[j])
if (minWeight + weights[v][j] < smallestWeight[j])
smallestWeight[j] = minWeight + weights[v][j];
}} //end shortestPath
Algorithm for all pair shortest path
§ All Pairs Shortest Paths
§ One solution is to run Dijkstra’s algorithm n = |V|
times, once for each vertex as the source. The total
running time would be O(n3)
§ This is suitable for sparse graphs
§ For dense graphs it is more efficient to use
specialized algorithms
Applications of Graphs
§ Analysis of electrical circuits, mobile networks,
social media networks, transportation networks,
water/power/Internet supply networks,
autonomous vehicles and so on.
§ Development of project schedules
§ Representation of analysis and design models
42

Assignment 2
Location Price
1 Penrith $0.25
2 Kingswood $0.00
3 Mount Druitt $0.00
4 Rooty hill $0.40
5 Blacktown $0.00
6 Parramatta $0.45
7 Olympic Park $0.60
8 Burwood $0.49
9 Colebee $0.49
10 Bedmond $0.30
11 Baulkham Hills $0.00
12 West Pennant Hills $0.49
13 Macquarie Pk $0.30
14 North Syndney $0.50
15 Central $0.55
16 Mascot $0.33
17 Kingsgrove $0.58
18 Bankstown $0.50
19 Liverpool $0.49
20 Campbelltown $0.53
43

Reading
§ Read textbook Chapter 12
§ Download example code from vUWS and read it, which is
extremely import for assignment 2.
§ Assignment 2 is an application to graph. All the
algorithms are existing, either provided by the lectures or
accessible from the Internet. You need to fully understand
them to apply these algorithms, which takes time. Once
you understand them, coding becomes easy. Practicals 7 &
8 contains some training for the algorithms.

You might also like