Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- Graphs In Data Structure by Anuj Modi 27282 views
- Graph in data structure by Abrish06 4154 views
- Lecture8 data structure(graph) by Taibah University... 18290 views
- Data Structures - Lecture 10 [Graphs] by Muhammad Hammad W... 2088 views
- Graph representation by Tech_MX 19796 views
- Graph data structure by Tech_MX 8073 views

No Downloads

Total views

2,761

On SlideShare

0

From Embeds

0

Number of Embeds

2

Shares

0

Downloads

193

Comments

0

Likes

3

No embeds

No notes for slide

- 1. Graphs
- 2. What is a graph?• A data structure that consists of a set of nodes(vertices) and a set of edges that relate the nodesto each other• The set of edges describes relationships among thevertices
- 3. Formal definition of graphs• A graph G is defined as follows:G=(V,E)V(G): a finite, nonempty set of verticesE(G): a set of edges (pairs of vertices)
- 4. Directed vs. undirected graphs• When the edges in a graph have nodirection, the graph is called undirected
- 5. • When the edges in a graph have a direction,the graph is called directed (or digraph)Directed vs. undirected graphs(cont.)E(Graph2) = {(1,3) (3,1) (5,9) (9,11)(5,7)Warning: if the graph isdirected, the order of thevertices in each edge isimportant !!
- 6. • Trees are special cases of graphs!!Trees vs graphs
- 7. Graph terminology• Adjacent nodes: two nodes are adjacent ifthey are connected by an edge• Path: a sequence of vertices that connecttwo nodes in a graph• Complete graph: a graph in which everyvertex is directly connected to every othervertex5 is adjacent to 77 is adjacent from 5
- 8. • What is the number of edges in a completedirected graph with N vertices?N * (N-1)Graph terminology (cont.)
- 9. • What is the number of edges in a completeundirected graph with N vertices?N * (N-1) / 2Graph terminology (cont.)
- 10. Graph terminology (cont.)• Multi-graph• Cycle• Loop
- 11. • Weighted graph: a graph in which each edgecarries a valueGraph terminology (cont.)
- 12. Graph implementation• Array-based implementation– A 1D array is used to represent the vertices– A 2D array (adjacency matrix) is used torepresent the edges
- 13. Array-based implementation-Adjacency Matrix
- 14. Graph implementation (cont.)• Linked-list implementation– A 1D array is used to represent the vertices– A list is used for each vertex v which contains thevertices which are adjacent from v (adjacency list)
- 15. Linked-list implementation
- 16. Adjacency Matrix for Un-Weighted Graphs1. Adjacency Matrix2. Matrix for edges3. Matrix for 2 length paths4. Matrix for 3 length paths5. Matrix for n length paths6. Path Matrix7. Completely Connected Graph
- 17. Graph searching• Problem: find a path between two nodes ofthe graph (e.g., Austin and Washington)• Methods: Depth-First-Search (DFS) orBreadth-First-Search (BFS)
- 18. Depth-First-Search (DFS)• What is the idea behind DFS?– Travel as far as you can down a path– Back up as little as possible when you reach a"dead end" (i.e., next vertex has been "marked"or there is no next vertex)• DFS can be implemented efficiently using astack
- 19. DFS• Algorithm:Starting Node is A1. Initialize all nodes to ready state(Status=1)2. Push starting nose A onto Stack and change its status towaiting state (Status=2).3.Repeat steps 4 & 5 until STACK is empty.4. Pop Top node N. Process N and change its Status to the processedstate (Status=3).5.Push onto Stack all the neighbors of N that are still in ready stateand change their state as waiting state(State=2).6. Exit.
- 20. Breadth-First-Searching (BFS)• What is the idea behind BFS?– Look at all possible paths at the same depthbefore you go at a deeper level– Back up as far as possible when you reach a"dead end" (i.e., next vertex has been "marked"or there is no next vertex)
- 21. BFS• Algorithm:Starting Node is A1. Initialize all nodes to ready state(Status=1)2. Push starting nose A onto Queue and change its status towaiting state (Status=2).3.Repeat steps 4 & 5 until Queue is empty.4. Remove the front node N. Process N and change its Status to theprocessed state (Status=3).5.Add to the rear of Queue all the neighbors of N that are still inready state and change their state to waiting (State=2).6. Exit.
- 22. • Examples of Both DFS & BFS.

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment