data structures and algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016...

28
Associate Professor Dr. Raed Ibraheem Hamed University of Human Development, College of Science and Technology Computer Science Department 2015 2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1

Upload: others

Post on 23-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Associate Professor Dr. Raed Ibraheem Hamed

University of Human Development, College of Science and

Technology Computer Science Department

2015 – 2016

Advanced Data Structures and Algorithms

Department of Computer Science - UHD 1

Page 2: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

What this Lecture is about:

Graph Concepts

Graphs

Directed and Undirected Graphs

Undirected Graph

Directed Graph (Digraph)

complete graph

incomplete graph

Page 3: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Graph Concepts

Graph terminology: vertex, edge, adjacent, incident,

degree, cycle, path, connected component

Types of graphs: undirected, directed, weighted

Graph representations: adjacency matrix, array

adjacency lists, linked adjacency lists

Graph search methods: breath-first, depth-first search

Department of Computer Science - UHD 2

Page 4: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Graphs

G = (V,E)

V is the vertex set.

Vertices are also called nodes and points.

E is the edge set.

Each edge connects two vertices.

Edges are also called arcs and lines.

Vertices i and j are adjacent vertices iff (i, j ) has an edge

in the graph

Department of Computer Science - UHD 3

Page 5: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Directed graph:

< v1, v2 > in E is ordered, i.e., a relation (v1,v2)

Undirected graph:

< v1, v2 > in E is un-ordered, i.e., a set { v1, v2 }

Degree of a node X: Out-degree: number of edges < X, v2 >

In-degree: number of edges < v1, X >

Degree: In-degree + Out-degree

Directed and Undirected Graphs

Department of Computer Science - UHD 4

Page 6: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Graphs

Undirected edge has no orientation (no arrow head)

Directed edge has an orientation (has an arrow head)

Undirected graph – all edges are undirected

Directed graph – all edges are directed

Department of Computer Science - UHD 5

u v

directed edge

u v

undirected edge

tail head

Page 7: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Applications – Communication Network

Page 8: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Applications – Communication Network

vertex = Router

edge = Communication link

Department of Computer Science - UHD 9

Page 9: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Applications - Driving Distance/Time Map

vertex = City

edge weight = Driving Distance/Time

Department of Computer Science - UHD 10

Page 10: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Applications - Street Map

• Streets are one- or two-way.

• A single directed edge denotes a one-way street

• A two directed edge denotes a two-way street

Department of Computer Science - UHD 11

vertex = Places

edge = Street

Page 11: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

A “Real-life” Example of a Graph

• V=set of 6 people: John, Mary, Joe, Helen,

Tom, and Paul, of ages 12, 15, 12, 15, 13,

and 13, respectively.

• E ={(x, y) | if x is younger than y}

John Joe

Mary Helen

Tom Paul

Department of Computer Science - UHD 12

Page 12: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Examples for Graph

0

1 2

3

0

1

2

0

1 2

3 4 5 6G1

G2

G3

V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}

V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}

V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}

complete undirected graph: n(n-1)/2 edges

complete directed graph: n(n-1) edges

complete graph incomplete graph

Page 13: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

0 0

1 2 3

1 2 0

1 2

3

(i) (ii) (iii) (iv)

(a) Some of the subgraph of G1

0

1 2

3

G1

Subgraphs of G1

Page 14: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

0 0

1

0

1

2

0

1

2

(i) (ii) (iii) (iv)

(b) Some of the subgraph of G3

0

1

2

G3

Subgraphs of G3

Page 15: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Intuition Behind Graphs

• The nodes represent entities (such as people, cities,

computers, words, etc.)

• Edges (x,y) represent relationships between entities x and

y, such as:

– “x loves y”

– “x hates y”

– “x is a friend of y” (note that this not necessarily reciprocal)

– “x considers y a friend”

– “x is a child of y”

– “x is a half-sibling of y”

– “x is a full-sibling of y”

• In those examples, each relationship is a different graph

Department of Computer Science - UHD 13

Page 16: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Graph Representation

• For graphs to be computationally useful,

they have to be conveniently represented in

programs

• There are two computer representations of

graphs:

– Adjacency matrix representation

– Adjacency lists representation

Department of Computer Science - UHD 14

Page 17: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Adjacency Matrix Representation

• In this representation, each graph of n nodes

is represented by an n x n matrix A, that is,

a two-dimensional array A

• The nodes are (re)-labeled 1,2,…,n

• A[i][j] = 1 if (i, j) is an edge

• A[i][j] = 0 if (i, j) is not an edge

Department of Computer Science - UHD 15

Page 18: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Example of Adjacency Matrix

01

4

2

3

0 1 0 1 0

0 0 1 0 0

0 0 1 0 0

1 0 0 0 0

0 0 0 1 0

A =

Department of Computer Science - UHD 16

Page 19: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Another Example of Adj. Matrix

John 2

Joe 3

Mary 0 Helen 1

Tom 4 Paul 5

0 0 0 0 0 0

0 0 0 0 0 0

1 1 0 0 1 1

1 1 0 0 1 1

1 1 0 0 0 0

1 1 0 0 0 0

A =

Department of Computer Science - UHD 17

Page 20: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Representing Graphs

Assume V = {1, 2, …, n}

An adjacency matrix represents the graph as a n x n

matrix A:

– A[i, j] = 1 if edge (i, j) E (or weight of edge)

= 0 if edge (i, j) E

Page 21: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Graphs: Adjacency Matrix

Example:

1

2 4

3

a

d

b c

A 1 2 3 4

1 0 1 1 0

2 0 0 1 0

3 0 0 0 0

4 0 0 1 0

Page 22: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Pros and Cons of Adjacency Matrices

• Pros:

– Simple to implement

– Easy and fast to tell if a pair (i, j) is an edge:

simply check if A[i][j] is 1 or 0

• Cons:

– No matter how few edges the graph has, the

matrix takes O(n2) in memory

Department of Computer Science - UHD 18

Page 23: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Adjacency Lists Representation

• A graph of n nodes is represented by a one-

dimensional array L of linked lists, where

– L[i] is the linked list containing all the nodes

adjacent from node i.

– The nodes in the list L[i] are in no particular

order

Department of Computer Science - UHD 19

Page 24: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Example of Linked Representation

L[0]: empty

L[1]: empty

L[2]: 0, 1, 4, 5

L[3]: 0, 1, 4, 5

L[4]: 0, 1

L[5]: 0, 1

John 2Joe

3

Mary 0 Helen 1

Tom 4 Paul 5

Department of Computer Science - UHD 20

Page 25: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Examples

1 3

2 2

3 1 2

1

3

2

32

1

4

1 2 3

2 1

3 1

4

Undirected graphs:

Each edge is represented twice

Department of Computer Science - UHD 21

Page 26: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Pros and Cons of Adjacency Lists

• Pros:

– Saves on space (memory): the representationtakes as many memory words as there are nodesand edge.

• Cons:

– It can take up to O(n) time to determine if a pairof nodes (i,j) is an edge: one would have tosearch the linked list L[i], which takes timeproportional to the length of L[i].

Department of Computer Science - UHD 22

Page 27: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Example of Representations

Linked Lists:

L[0]: 1, 2, 3

L[1]: 0, 2, 3

L[2]: 0, 1, 3

L[3]: 0, 1, 2

L[4]: 5

L[5]: 4

John 2 Joe 3

Mary 0 Helen 1

Tom 4 Paul 5

0 1 1 1 0 0

1 0 1 1 0 0

1 1 0 1 0 0

1 1 1 0 0 0

0 0 0 0 0 1

0 0 0 0 1 0

A =

Adjacency Matrix:

Department of Computer Science - UHD 23

Page 28: Data Structures and Algorithmsraedh.weebly.com/uploads/1/3/4/6/13465115/lectures_of...2015 –2016 Advanced Data Structures and Algorithms Department of Computer Science - UHD 1 What

Department of Computer Science - UHD 24

Thank you ???