Upcoming SlideShare
×

# Data structures and algorithms lab7

1,618 views

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,618
On SlideShare
0
From Embeds
0
Number of Embeds
853
Actions
Shares
0
24
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Data structures and algorithms lab7

1. 1. DATA STRUCTURES AND ALGORITHMS LAB 7 Bianca Tesila FILS, March 2014
2. 2. OBJECTIVES  Graphs  DFS  BFS  Bipartite Graphs
3. 3. GRAPHS: INTRODUCTION  Pair of sets: G = (V, E)  V = the set of vertices  E = the set of edges; E ⊆ V x V
4. 4. GRAPHS: INTRODUCTION  Directed/ Undirected
5. 5. GRAPHS: INTRODUCTION  Weighted/ Unweighted
6. 6. GRAPHS: INTRODUCTION  Cyclic/ Acyclic
7. 7. GRAPHS: REPRESENTATION  Adjacency Matrix  A V x V matrix, with AdjacencyMatrix[i][j] storing whether there is an edge between the ith vertex and the jth vertex or not  Linked List of Neighbours  One linked list per vertex, each storing directly reachable vertices !! What are the advantages and disadvantages of each of them?
8. 8. GRAPHS: TRAVERSALS Depth-First Search - Go as far as you can (if you have not visited that node yet), otherwise, go back and try another way  There is no source vertex  All the vertices are traversed DFS (vertex u) { mark u as visited for each vertex v directly reachable from u if v is unvisited DFS (v) } !! Initially, all the vertices are marked as unvisited.
9. 9. GRAPHS: TRAVERSALS !! Exercise:  Emily wants to distribute candies to N students, one by one, with a rule that if student A is teased by B, A can receive the candy before B.  Given the lists of students teased by each student, find a possible sequence to give the candies.
10. 10. GRAPHS: TRAVERSALS Breadth-First Search - Instead of going as far as possible, BFS tries to search all paths.  Uses a start vertex for the traversal: s  Determine the minimum number of edges (shortest path considering that all the edges have the same weight = 1) between the source s and all the other vertices of the graph  Not all the vertices are traversed  BFS makes use of a queue to store visited (but not dead) vertices, expanding the path from the earliest visited vertices while queue Q not empty dequeue the first vertex u from Q for each vertex v directly reachable from u if v is unvisited enqueue v to Q mark v as visited !! Initially, all the vertices, except for the start vertex, are marked as unvisited and the queue contains only the start vertex .
11. 11. GRAPHS: TRAVERSALS !! Exercise: Let’s consider un undirected graph, representing a social network. Given an user, display all his friends (or information about them) having the degree <=N (N is given). A is friend with B if there is an edge between A and B; we say that the degree of friendship is 1. Friends of friends have the degree of friendship 2.
12. 12. GRAPHS: BIPARTITE GRAPHS  A graph whose vertices can be divided into two disjoint sets such that every edge connects a vertex in U to one in V.
13. 13. GRAPHS: BIPARTITE GRAPHS !! Exercise: Check if a graph is bipartite and if so, display the components of those two sets U and V. The graph will be represented by the lists of neighbors. Check your code for the following graphs:  G1=({ 1,2,3,4,5,6,7,8,9},{ 12, 13, 45, 56, 75, 24, 58, 79, 43, 89})  G2=({ 1,2,3,4,5,6,7,8,9},{ 12, 13, 45, 56, 75, 24, 58, 79, 43, 89,47})
14. 14. HOMEWORK Finish all the lab assignments.