SlideShare une entreprise Scribd logo
1  sur  14
DATA STRUCTURES AND ALGORITHMS
LAB 7
Bianca Tesila
FILS, March 2014
OBJECTIVES
 Graphs
 DFS
 BFS
 Bipartite Graphs
GRAPHS: INTRODUCTION
 Pair of sets: G = (V, E)
 V = the set of vertices
 E = the set of edges; E ⊆ V x V
GRAPHS: INTRODUCTION
 Directed/ Undirected
GRAPHS: INTRODUCTION
 Weighted/ Unweighted
GRAPHS: INTRODUCTION
 Cyclic/ Acyclic
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?
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.
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.
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 .
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.
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.
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})
HOMEWORK
Finish all the lab assignments.

Contenu connexe

Tendances

Adjacency list
Adjacency listAdjacency list
Adjacency list
Stefi Yu
 
Skiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first searchSkiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first search
zukun
 

Tendances (20)

Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 
BFS
BFSBFS
BFS
 
Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Graphs
GraphsGraphs
Graphs
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Graphs in Data Structure
Graphs in Data StructureGraphs in Data Structure
Graphs in Data Structure
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Adjacency list
Adjacency listAdjacency list
Adjacency list
 
Skiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first searchSkiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first search
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Graphss
GraphssGraphss
Graphss
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
 

En vedette

Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1
Bianca Teşilă
 
Push down automata
Push down automataPush down automata
Push down automata
Somya Bagai
 
0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming
Maher Alshammari
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
danhumble
 
structure and union
structure and unionstructure and union
structure and union
student
 
Lecture 04 normalization
Lecture 04 normalization Lecture 04 normalization
Lecture 04 normalization
emailharmeet
 

En vedette (20)

Structure & union
Structure & unionStructure & union
Structure & union
 
Normalization
NormalizationNormalization
Normalization
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiMinimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumi
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1
 
Normalization
NormalizationNormalization
Normalization
 
Pda
PdaPda
Pda
 
Push down automata
Push down automataPush down automata
Push down automata
 
0 1 knapsack problem
0 1 knapsack problem0 1 knapsack problem
0 1 knapsack problem
 
0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
My presentation minimum spanning tree
My presentation minimum spanning treeMy presentation minimum spanning tree
My presentation minimum spanning tree
 
Push Down Automata (PDA)
Push Down Automata (PDA)Push Down Automata (PDA)
Push Down Automata (PDA)
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
structure and union
structure and unionstructure and union
structure and union
 
Normalization
NormalizationNormalization
Normalization
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
Normalization
NormalizationNormalization
Normalization
 
DBMS - Normalization
DBMS - NormalizationDBMS - Normalization
DBMS - Normalization
 
Lecture 04 normalization
Lecture 04 normalization Lecture 04 normalization
Lecture 04 normalization
 

Similaire à Data structures and algorithms lab7

Similaire à Data structures and algorithms lab7 (20)

Graphs (1)
Graphs (1)Graphs (1)
Graphs (1)
 
Talk on Graph Theory - I
Talk on Graph Theory - ITalk on Graph Theory - I
Talk on Graph Theory - I
 
Graphs
GraphsGraphs
Graphs
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
 
Graphs
GraphsGraphs
Graphs
 
NON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxNON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptx
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
 
Graph
GraphGraph
Graph
 
lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.ppt
 
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
8-Graph.ppt
8-Graph.ppt8-Graph.ppt
8-Graph.ppt
 
Daa chpater 12
Daa chpater 12Daa chpater 12
Daa chpater 12
 
Analysis &amp; design of algorithm
Analysis &amp; design of algorithmAnalysis &amp; design of algorithm
Analysis &amp; design of algorithm
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 
Graphs
GraphsGraphs
Graphs
 

Plus de Bianca Teşilă

Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11
Bianca Teşilă
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10
Bianca Teşilă
 
Data structures and algorithms lab9
Data structures and algorithms lab9Data structures and algorithms lab9
Data structures and algorithms lab9
Bianca Teşilă
 
Data structures and algorithms lab8
Data structures and algorithms lab8Data structures and algorithms lab8
Data structures and algorithms lab8
Bianca Teşilă
 
Data structures and algorithms lab6
Data structures and algorithms lab6Data structures and algorithms lab6
Data structures and algorithms lab6
Bianca Teşilă
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
Bianca Teşilă
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4
Bianca Teşilă
 
Data structures and algorithms lab3
Data structures and algorithms lab3Data structures and algorithms lab3
Data structures and algorithms lab3
Bianca Teşilă
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2
Bianca Teşilă
 

Plus de Bianca Teşilă (10)

Akka Streams - An Adobe data-intensive story
Akka Streams - An Adobe data-intensive storyAkka Streams - An Adobe data-intensive story
Akka Streams - An Adobe data-intensive story
 
Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10
 
Data structures and algorithms lab9
Data structures and algorithms lab9Data structures and algorithms lab9
Data structures and algorithms lab9
 
Data structures and algorithms lab8
Data structures and algorithms lab8Data structures and algorithms lab8
Data structures and algorithms lab8
 
Data structures and algorithms lab6
Data structures and algorithms lab6Data structures and algorithms lab6
Data structures and algorithms lab6
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4
 
Data structures and algorithms lab3
Data structures and algorithms lab3Data structures and algorithms lab3
Data structures and algorithms lab3
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Data structures and algorithms lab7

  • 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
  • 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.