Your SlideShare is downloading. ×
Data structures and algorithms lab7
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Data structures and algorithms lab7


Published on

Published in: Technology, Education

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. DATA STRUCTURES AND ALGORITHMS LAB 7 Bianca Tesila FILS, March 2014
  • 2. OBJECTIVES  Graphs  DFS  BFS  Bipartite Graphs
  • 3. GRAPHS: INTRODUCTION  Pair of sets: G = (V, E)  V = the set of vertices  E = the set of edges; E ⊆ V x V
  • 4. GRAPHS: INTRODUCTION  Directed/ Undirected
  • 5. GRAPHS: INTRODUCTION  Weighted/ Unweighted
  • 6. GRAPHS: INTRODUCTION  Cyclic/ Acyclic
  • 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. 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. 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. 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. 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. 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. 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. HOMEWORK Finish all the lab assignments.