0% found this document useful (0 votes)
15 views20 pages

csc2001f 2024 9 Graphs

CSC2001 graphs

Uploaded by

hmdprkr
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)
15 views20 pages

csc2001f 2024 9 Graphs

CSC2001 graphs

Uploaded by

hmdprkr
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/ 20

Graphs

CSC2001F Data Structures


Jan Buys <[email protected]>

Department of Computer Science


School of IT
University of Cape Town

2024
(Based on slides from Hussein Suleman)
Graphs
 A graph is a set of nodes/vertices and a set of edges connecting
vertices
 V: set of all vertices/nodes
 E: set of all edges. Each edge (v,w) ∈E connects two nodes v and w
 G = (V, E)

 Graphs are more general than trees: no constraints on edges

department of computer science


Graph Visualized
 V={A,B,D,G,E,F}
B
A  E={(A,B),(A,D),
(A,E),(B,G),(D,B),
(D,E),(D,F),(G,F),
D G
(E,F)}
 |V|=6

E
 |E|=9
F

department of computer science


Graph Examples
 Transportation networks

https://groundup.org.za/article/which-parts-of-metrorail-are-still-closed/
department of computer science
Graph Examples
 Knowledge graphs

department of computer science


Graph Examples
 Social network graphs

https://upload.wikimedia.org/wikipedia/commons/9/9b/Social_Network_Analysis_Visualization.png

department of computer science


Why Graphs
 More problem domains:
 Internet architecture
 Task dependencies in operating systems
 Electricity distribution

 We can use graphs to solve important problems:


 Find shortest path between nodes
 Analyse community structures
 Find related information

department of computer science


Some Types of Graphs
B B
A A

D D

Directed, unweighted Undirected, unweighted

4 B 4 B
A A
2 2
5 5
D D

Directed, weighted Undirected, weighted

department of computer science


Definitions
 A node w is adjacent to v if (v,w)∈E
 A path is a sequence of nodes connected by edges
 The length of a path is the number of edges (unweighted) or the sum
of costs (weighted)
 A cycle is a path that begins and ends at the same node and has at
least 1 edge
 A directed acyclic graph (DAG) is directed and has no (directed) cycles
 A dense graph has many edges – a sparse graph has relatively few
edges
 In most applications, graphs are sparse:|E| is typically much less
than |V|2 (all possible edges)

department of computer science


Storing Graphs: Adjacency Matrix

4 A B D E F G
B
A A 4 1 5
1
1 2 B 1
D G D 2 3 8
5
8
E 4
3
2
F
E F
4 G 2

department of computer science


Storing Graphs: Adjacency Lists
A: (B,4) (D,1) (E,5)
4 B
A B: (G,1)
1
1 2 D: (B,2) (E,3) (F,8)
D G
E: (F, 4)
5
8 F:
3
2 G: (F, 2)
E F
4
Lists are better for sparse
graphs – use less memory
department of computer science
Graph traversal
 Visit all the nodes in a (connected) graph
 Find the shortest path from a given node to all other nodes
 Assume an unweighted graph: weights are equal for every
edge (weights are nominally 1)
 Basic idea: Perform a breadth-first search (BFS) using a
queue to maintain list of nodes to visit
 Generalization of level order tree traversal

department of computer science


BFS: Algorithm
 Add start node to queue with 0 distance
 While queue is not empty:
 Get node v from queue
 For each edge from v to w:
 If distance to w is infinity (w not yet processed)
 Distance to w = distance to v + 1
 Path to w goes through v
 Add w to queue

department of computer science


BFS: Example 1/5
0,null Queue: A
B
A
Distance to A set to 0
D G
A has no previous node
All other distances set to
infinity
E F

department of computer science


BFS: Example 2/5
0,null Dequeue A; Update distances
1,A
B for B=1, D=1, E=1; previous
A
node in each case is A;
enqueue B/D/E;
D 1,A G

Queue: B, D, E

E F
1,A

department of computer science


BFS: Example 3/5
0,null Dequeue B; Update distance
1,A
B for G=2; previous node is B;
A
enqueue G
2,B
D 1,A G
Queue: D, E, G

E F
1,A

department of computer science


BFS: Example 4/5
0,null Dequeue D; Update distance
1,A
B for F=2; previous node is D;
A
enqueue F
2,B
D 1,A G
Queue: E, G, F

E F 2,D
1,A

department of computer science


BFS: Example 5/5
Dequeue E
0,null 1,A
B (already processed – not infinity)
A
Dequeue G
2,B
(already processed – not infinity)
D 1,A G
Dequeue F
(already processed – not infinity)
No more items to process
E F 2,D
1,A

department of computer science


BFS: Code

From Weiss

department of computer science


BFS: Complexity
 O(|V| + |E|)
 Why:
 We process each vertex once.
 We process each edge once.

 If the graph is dense, then this could become O(|V|2)


because |E| is then close to |V|2.

department of computer science

You might also like