Sei sulla pagina 1di 15

CS211-Algorithms & Data

Structures
Taibah University

Dr. Sameer M. Alrehaili


College of Science and Computer Engineering, Yanbu

1
Graphs

2
Linear data structures

● Where data elements are arranged sequentially and attached to its previous
or next adjacent..

3
Linear vs non-linear data structures

Elements Data elements are arranged Data elements are arranged


in sequential manner in hierarchical manner

Traversing Can be traversed In a single It is not possible in a single


run run

Implementation easy complex

Levels involved Single level Multiple levels

Examples Array, Queue, Stack, Linked Trees and graphs


list

Memory utilisation inefficient efficient

4
What is a graph?

● A graph is a set of nodes ( also called vertices or points), displayed as nodes,


and edges (also called lines, links or, in directed graphs, arc), displayed as
connections between the nodes.

A graph G is an ordered pair of a set V of vertices and a set of E of edges

G=(V, E)
u v u v
directed undirected
(u, v) {u,v}

V={1,2,3,4,5,6}

E={{1,2},{1,5},{2,3},{2,5},{5,4},{4,3},{4,6}}

5
Properties of graphs

● For directed graph


○ 0<= |E| <=n(n-1)
○ The maximum number of edges is n(n-1)
○ The minimum number of edges is 0 , |E| =0
○ It can has self loop and multi edge
● For undirected graph
○ 0<= |E| <= (n(n-1))/2
● A graph is called dense if the number edges in the graph is close to
maximum number
● A graph is called sparse if the number of edges is less
● Dence graph is represented by adjacency matrix
● Sparse graph is represented by adjacency list
● A path is sequence of nodes where each adjacent pair in the sequence is
connected by an edge. <A, B, C>
● Simple path, when edges are not repeated

6
Adjacency matrix

● Undirected graph repeats

7
Adjacency list

8
Weighted vs Unweighted

● Graph edges are labeled with weight in order to calculate the cost of a certain
path.
● For example, find the shortest path from Prague to Buenos Aires?

9
Applications

● Many real world problems can be modeled using a graph.


● Can be used to represent data has some relationship
● Can you suggest some friends to Patrick?
● The WWW can be thought of as directed graph, in which the vertices
represent web pages, and the directed edges hyperlinks.
● Social network can be represented as undirected graph.
● Google crawlers are just graph traversal

10
Complexity

● Time and space complexity are measures in terms of:


○ b maximum branching factor
○ d depth of the least cost solution
○ m maximum depth (May be ∞ )

11
Finding shortest path

● Uninformed Search
○ Breadth-first search (BFS) (undirected graph) such as maze (Frontier is FIFO queue)
○ Depth-first search (DFS) (Frontier is FIFO stack)
● Informed
○ Dijkstra’s algorithm can be used on weighted edges
○ Greedy search
○ A* (used in games programming, NLP, financial trading systems, and space exploration )

12
Dijkstra’s algorithm

● The objective of Dijkstra’s algorithm is to find the shortest path between any
two vertices in a graph.
● It will find the shortest path from given start vertex to every other vertex in the
graph
● I uses Two list
● One to tracking the vertices that we visited.
● Another to keep track the unvisited vertices
Vertex Shortest Previous
path vertex
from A

6 A 0
A B
5 B 3 D

1 2 C 7 E
2 C
D 1 A

5 E 2 D
D E
13
1
Dijkstra’s algorithm

1. Let distance of start vertex from start vertex =0


2. Let distance of start vertex from start vertex = ∞
3. Repeat
a. Visit the unvisited vertex with the smallest known distance from the start vertex
b. For the current vertex, examine its unvisited neighbours
c. For the current vertex, calculate distance of each neighbour from start vertex
d. If the calculated distance of a vertex is less than the known distance, update the shortest
distance
e. Update the previous vertex for each of the updated distances
f. Add the current vertex to the list of visited vertices
4. Until all vertices visited

14
Implementation

6 Vertex Shortest Previous


A B path vertex
5 from A

1 2 A 0
2 C
B 3 D

D E 5 C 7 E
1 D 1 A

E 2 D

visited=[A,D,E,B,C] unvisited=[]

15

Potrebbero piacerti anche