• Tidak ada hasil yang ditemukan

PPT PowerPoint Presentation

N/A
N/A
Protected

Academic year: 2025

Membagikan "PPT PowerPoint Presentation"

Copied!
49
0
0

Teks penuh

(1)

Searching in a Graph

(2)

Representations of Graphs

• Adjacency Matrices

• Adjacency Lists

(3)

Adjacency Matrices

Graphs G = (V, E) can be represented by adjacency matrices

G[v1..v|V |, v1..v|V |], where the rows and columns are indexed by the nodes, and the entries G[vi, vj] represent the edges. In the case of unlabeled graphs, the entries are just boolean values.

        

A B C D

A 0 1 1 1

B 1 0 0 1

C 1 0 0 1

D 1 1 1 0

(4)

Adjacency Matrices

In case of labeled graphs, the labels themselves may be introduced into the entries.

         

A B C D A

 

10 4 1 B

 

 

 

15 C

 

 

 

9 D

 

 

 

 

Adjacency matrices require O(|V |2) space, and so they are space-efficient only when they are dense (that is, when the graphs have many edges). Time-wise, the adjacency matrices allow easy addition and deletion of edges.

(5)

Adjacency Lists

A representation of the graph consisting of a list of nodes, with each node containing a list of its neighboring nodes.

                   

This representation takes O(|V | + |E|) space.

(6)

Graph Traversal

• Depth-First Traversal

• Breadth-First Traversal

(7)

Depth-First Traversal

• algorithm    dft(x)      

visit(x)      

FOR   each   y   such   that   (x,y)   is   an   edge   DO  

     

IF  < y   was   not   visited   yet  >

THEN            dft(y)  

(8)

Depth-First Traversal

• A recursive algorithm implicitly recording a

“backtracking” path from the root to the

node currently under consideration

(9)

Depth-First Traversal

(10)

Depth-First Traversal

(11)

Depth-First Traversal

(12)

Depth-First Traversal

(13)

Depth-First Traversal

•Depth first search is another way of traversing

graphs, which is closely related to preorder traversal of a tree.

•the Breath-first search tree is typically "short and

bushy", the DFS tree is typically "long and stringy".

(14)

Breadth-First Traversal

Visit the nodes at level i before the nodes of level i+1.

         

(15)

Breadth-First Traversal

visit(start   node)  

queue   <-   start   node  

WHILE   queue   is   not   empty   DO      x   <-   queue     

FOR   each   y   such   that   (x,y)   is   an   edge  

       and   y   has   not   been   visited   yet   DO       

visit(y)        queue   <-   y      END  

END  

(16)

Breadth-First Traversal

(17)

Breadth-First Traversal

(18)

Breadth-First Traversal

(19)

Breadth-First Traversal

• Each vertex is clearly

• marked at most once,

• added to the list at most once (since that happens only when it's marked), and

• removed from the list at most once.

Since the time to process a vertex is proportional to the length of its adjacency list, the total time for the whole algorithm is

O(m).

• A tree T constructed by the algorithm is called a breadth first search tree.

• The traversal goes a level at a time, left to right within a level

(where a level is defined simply in terms of distance from the root of the tree).

(20)

Breadth-First Traversal

• Every edge of G can be classified into one of three groups.

• Some edges are in T themselves.

• Some connect two vertices at the same level of T.

• The remaining ones connect two vertices on two adjacent levels. It is not possible for an edge to skip a level.

• Breadth-first search tree really is a shortest path tree starting from its root.

(21)

Relation between BFS and DFS

bfs(G) {

list L = empty tree T = empty

choose a starting vertex x search(x)

while(L nonempty)

remove edge (v,w) from start of L

if w not yet visited {

add (v,w) to T search(w)

} }

dfs(G) {

list L = empty tree T = empty

choose a starting vertex x search(x)

while(L nonempty)

remove edge (v,w) from end of L

if w not yet visited {

add (v,w) to T search(w)

}

search(vertex v) { }

visit(v);

for each edge (v,w)

add edge (v,w) to end of L }

(22)

Relation between BFS and DFS

Both of these search algorithms now keep a list of edges to explore; the only difference between the two is

while both algorithms adds items to the end of L,

•BFS removes them from the beginning, which results in maintaining the list as a queue

•DFS removes them from the end, maintaining the list

as a stack.

(23)

BFS and DFS in directed graphs

The same search routines work essentially unmodified for directed graphs.

The only difference is that when exploring a vertex v, we only want to look at edges (v,w) going out of v; we ignore the other edges coming into v.

For BFS in directed graphs each edge of the graph either

•connects two vertices at the same level

•goes down exactly one level

•goes up any number of levels.

For DFS, each edge either connects an ancestor to

•a descendant

•a descendant to an ancestor

•one node to a node in a previously visited subtree.

(24)

Searching

(25)
(26)

Searching

(27)

Searching

(28)

Searching

(29)
(30)

Searching

(31)

Searching

(32)
(33)

Searching

(34)

Searching

(35)

Searching

(36)
(37)
(38)
(39)
(40)
(41)
(42)
(43)
(44)
(45)
(46)
(47)
(48)

End

(49)

End

Gambar

Graph Traversal

Referensi

Dokumen terkait

• A software platform and operating system for mobile devices • Based on the Linux kernel • Developed by Google and later the Open Handset Alliance OHA • Allows writing managed code

INNOVATION AND NEW PRODUCT STRATEGY INNOVATION AND NEW PRODUCT STRATEGY • Innovation as a Customer Driven Process Innovation as a Customer Driven Process • New Product Planning New

The legator must be a Muslim “at the time of making or execution of the Will.” A Will operates only after the death of the legator; before his death, it is simply a mere

Change in Mass in Control Volume = Mass Flux In – Mass Flux Out Conservation of Mass The conservation of mass requires that the change in mass stored in a control volume over time t

THE PRINCIPLES IN MEDICAL ETHICS • The Principle of Non-Maleficence • The Principle of Beneficence • The Principle of Autonomy • The Principle of Veracity • The Principle of

Ethics Dilemmas Ethics covers the following dilemmas: • how to live a good life • our rights and responsibilities • the language of right and wrong • moral decisions - what is good

Purpose Classifying soils into groups with similar behavior, in terms of simple indices, can provide geotechnical engineers a general guidance about engineering properties of the soils

Chapter 13 Chapter 13 Summary Inductors in dc circuits When an inductor is connected in series with a resistor and dc source, the current change is exponential... Chapter 13 Chapter