0% found this document useful (0 votes)
10 views34 pages

Graphs

The document defines graphs and graph terminology. It describes different types of graphs and how to represent graphs. It also explains graph traversal algorithms like depth-first search and breadth-first search.

Uploaded by

jayden goh1000
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)
10 views34 pages

Graphs

The document defines graphs and graph terminology. It describes different types of graphs and how to represent graphs. It also explains graph traversal algorithms like depth-first search and breadth-first search.

Uploaded by

jayden goh1000
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/ 34

Graph

Learning Outcome
Successful students should be able to:
 Define graph and terminology
 Describe graph representation method
 Show how to the graph traversal
 Describe how to use graph in real world application
Linear data structure
Non Linear Data structure
What are graphs?
■ Set of nodes and connections between them.
Types of graphs
■ Undirected graph: A link is both ways
■ Example : Facebook (friends)
Types of graphs
■ Directed graph (Digraph): Each link is directional and
does not imply the inverse.
■ Eg: Web crawling

Note : edge (3,2) <> (2,3)


Types of graphs
Weighted graph:
■ The parameters along edges or at nodes are interval data
and can be summed and compared.
Types of graphs
■ Cyclic graphs: Has at least one “cycle”. That is, a path from
a node that leads back to itself.
Types of graphs
■ Directed acyclic graph: Links have direction.
– No cycles exist.
– Used a lot. Called a "DAG" for short. Possibly humorous
to Australians.
Terminology
■ The nodes are called vertices.

■ The links are called edges (sometimes arcs).

■ An edge is incident to/on a vertex if it connects it to


another. (Eg: incident of vertex 1 is {1,2}, {1,5}

■ Connected vertices are called adjacent or neighbours.

■ A vertex's degree is the number of edges incident on it.


Vertex Degree (Undirected Graph)
Vertex Degree (Directed Graph)
Your Turn : Identify
■ Vertices ?
■ Edges ?
■ Incident of A, C
■ Adjacent vertices A
■ Degree of vertex A, B, C, D, F
Representing graphs
■ We can represent vertices with integers (actually, any unique value, would
do for most of the times).

■ Edges are pairs of vertices, (a,b), connecting a and b.

■ A graph, G, has a set of vertices, V, and a set of edges, E.

■ We say G=(V,E).

■ We use n and m to represent the numbers of vertices and edges.

■ Think n for node if you find it hard to remember which way around these are.
Visualisation
■ A graph G=(V,E)
■ Where :
– V={1,2,3,4,5}
– E={{1,2},{2,4},{3,4},{3,5},{4,5}}
Implementing Graphs
■ There are two ways of implementing a graph when programming.

■ Adjacency Matrix:
– A two dimensional matrix of boolean values where the value
of a cell i, j is true if vertices i and j are connected.

■ Adjacency List:
– Each vertex contains a list of vertices that it is connected to.
Adjacency Matrix
■ Note reflection around i=j, due to undirectedness.

■ Implied False in empty cells.


Adjacency List
Weighted Graphs
■ Sometimes it’s useful to store a number with each edge.

■ This changes the way graphs are represented.

■ The adjacency matrix is now numerical instead of boolean.

■ Unconnected nodes can be given a default value, such as infinity for


shortest path finding.

■ The adjacency list must store the edges as pairs including the connection
and the weight.
Adjacency Matrix (2D dimensional array)
Adjacency List (Array of linked list)
Graph Traversal
■ Storing data in a graph is fairly easy.

■ They don't really become useful until we can


search for data or find information about the
interconnections.

■ Algorithms that interrogate the graph by


following the edges are called traversal
algorithms.
Depth-First Search (DFS)
■ Visit all nodes beginning with v in graph G.

■ Sometimes called Depth First Traversal (DFT).

■ depthFirstSearch(G,v)
– Not searching for v, even though this is
what it reads like…
Depth-First Search (DFS)
■ Traverses a graph by visiting each vertex in turn.

■ Finds the first edge for the current vertex.

■ Follows it to reach the next unvisited vertex.

■ Marks the vertex as visited.

■ Repeats until all vertices are marked as visited.


Depth-First Search (DFS) -
Pseudocode
DFS : 0 1 3 4 2

What other order could it be?


DFS Pseudocode
■ One of the output from the example graph is [1, 7, 5, 8, 6, 4, 3] when
beginning with vertex 1. 2 is never visited because no edges go to it.

■ Could have been different, but the edge ordering from each node is arbitrary.

■ What other order could it be?


DFS Video/Visualisation Tutorial

■ https://www.cs.usfca.edu/~galles/visualization/DFS.html

■ https://visualgo.net/en/dfsbfs?slide=1
Breadth-First Search (BFS)
■ Traverses the graph by searching every edge from the current
vertex.

■ Sometimes called Breadth First Traversal (BFT).

■ Only moves to the next vertex in the graph when all edges have
been explored.

■ The algorithm is the same as DFS except that a Queue is used to


store the list of vertices to visit.

■ Will find the shortest path.


BFS Pseudocode
BFS Undirected Graph
■ Output from our example graph is
[A,B,S,C,G,D,E,F,H] when beginning
with vertex A.

■ What other order could it be?


BFS Video/Visualisation Tutorial

■ https://www.cs.usfca.edu/~galles/visualization/BFS.html
Application of graph in real world application
Analysing related
Map analysis,
Ranking search data, such as
route finding,
results social networks,
path planning
proteins, etc.

Constraint Physics
Compiler
satisfaction simulations
optimisation
(timetabling) (actual physics)

Social
Physics
connections:
simulations
Facebook uses
(games)
graphs for this

You might also like