0
Upcoming SlideShare
×

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.
Standard text messaging rates apply

# Graphs

2,040

Published on

Published in: Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

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

Views
Total Views
2,040
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
0
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Transcript

• 2. Graph <ul><li>A data structure that consists of a set of nodes ( vertices ) and a set of edges that relate the nodes to each other </li></ul><ul><li>The set of edges describes relationships among the vertices </li></ul>
• 3. <ul><li>Node: </li></ul><ul><li>Vertex point junction </li></ul><ul><li>Edge: </li></ul><ul><li>Give path. </li></ul><ul><li>TYPES OF GRAPHS: </li></ul><ul><li>1)Directed </li></ul><ul><li>2)Undirected </li></ul>
• 4. Undirected graphs When the edges in a graph have no direction, the graph is called undirected <ul><ul><li>edges do not have a direction </li></ul></ul><ul><ul><ul><li>(V1, V2) and (V2, V1) are the same edge </li></ul></ul></ul>
• 5. <ul><li>When the edges in a graph have a direction, the graph is called directed (or digraph ) </li></ul>Directed graphs E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7) <ul><ul><li>edges have a direction </li></ul></ul><ul><ul><ul><li>&lt;V1, V2&gt; and &lt;V2, V1&gt; are different edges </li></ul></ul></ul>
• 6. Graph implementation <ul><li>Array-based implementation </li></ul><ul><ul><li>A 1D array is used to represent the vertices </li></ul></ul><ul><ul><li>A 2D array (adjacency matrix) is used to represent the edges </li></ul></ul>
• 7. Array-based implementation
• 8. <ul><li>Linked-list implementation </li></ul><ul><ul><li>A 1D array is used to represent the vertices </li></ul></ul><ul><ul><li>A list is used for each vertex v which contains the vertices which are adjacent from v (adjacency list) </li></ul></ul>
• 10. <ul><li>Traversing a graph </li></ul><ul><li>Where to start? </li></ul><ul><li>Will all vertices be visited? </li></ul><ul><li>How to prevent multiple visits? </li></ul>
• 11. Graph searching <ul><li>Depth-First-Search (DFS) or Breadth-First-Search (BFS) </li></ul>
• 12. depth first traversal <ul><li>depthFirstTraversal (v) </li></ul><ul><li>visit v </li></ul><ul><li>mark v as visited </li></ul><ul><li>for each neighbor (u) of v </li></ul><ul><li>if not yet visited </li></ul><ul><li>depthFirstTraversal (u) </li></ul>
• 13. <ul><li>Set found to false </li></ul><ul><li>stack.Push(startVertex) </li></ul><ul><li>DO </li></ul><ul><li>stack.Pop(vertex) </li></ul><ul><li>IF vertex == endVertex </li></ul><ul><li>Set found to true </li></ul><ul><li>ELSE </li></ul><ul><li>Push all adjacent vertices onto stack </li></ul><ul><li>WHILE !stack.IsEmpty() AND !found </li></ul><ul><li>  </li></ul><ul><li>IF(!found) </li></ul><ul><li>Write &amp;quot;Path does not exist&amp;quot; </li></ul>Depth-First-Search (DFS)
• 14. Depth-First-Search (DFS) <ul><li>What is the idea behind DFS? </li></ul><ul><ul><li>Travel as far as you can down a path </li></ul></ul><ul><li>DFS can be implemented efficiently using a stack </li></ul>
• 15. start end (initialization)
• 16. &nbsp;
• 17. &nbsp;
• 18. Breadth-First-Searching (BFS) <ul><li>What is the idea behind BFS? </li></ul><ul><ul><li>Look at all possible paths at the same depth before you go at a deeper level </li></ul></ul>
• 19. breadth first traversal <ul><li>Breadth First Traversal (v) </li></ul><ul><li>put v in Q </li></ul><ul><li>while Q not empty </li></ul><ul><li>remove w from Q </li></ul><ul><li>visit w </li></ul><ul><li>mark w as visited </li></ul><ul><li>for each neighbor (u) of w </li></ul><ul><li>if not yet visited </li></ul><ul><li>put u in Q </li></ul>
• 20. <ul><li>BFS can be implemented efficiently using a queue </li></ul><ul><ul><li>Set found to false </li></ul></ul><ul><ul><li>queue.Enqueue(startVertex) </li></ul></ul><ul><ul><li>DO </li></ul></ul><ul><ul><li>queue.Dequeue(vertex) </li></ul></ul><ul><ul><li>IF vertex == endVertex </li></ul></ul><ul><ul><li>Set found to true </li></ul></ul><ul><ul><li>ELSE </li></ul></ul><ul><ul><li>Enqueue all adjacent vertices onto queue </li></ul></ul><ul><ul><li>WHILE !queue.IsEmpty() AND !found </li></ul></ul><ul><li>  </li></ul>Breadth-First-Searching (BFS) IF(!found) Write &amp;quot;Path does not exist&amp;quot;
• 21. start end (initialization)
• 22. next:
• 23. &nbsp;
• 24. EXAMPLES OF GRAPHS IN OUR DAILY LIFE FACEBOOK ,TWITTER,OTHER SOCAIL NETWORKS
• 25. some problems that can be represented by a graph <ul><li>computer networks </li></ul><ul><li>airline flights </li></ul><ul><li>road map </li></ul><ul><li>course prerequisite structure </li></ul><ul><li>tasks for completing a job </li></ul><ul><li>flow of control through a program </li></ul><ul><li>many more </li></ul>
• 26. Types of Graphs <ul><li>SIMPLE GRAPH </li></ul><ul><li>At most one edge between any pair of nodes. </li></ul>
• 27. Notation of graphs <ul><li>A graph G is defined as follows: </li></ul><ul><li>G=(V,E) </li></ul><ul><li>V(G): a finite, nonempty set of vertices </li></ul><ul><li>E(G): a set of edges (pairs of vertices) </li></ul>
• 28. Multigraph <ul><li>Multiple edges between nodes are allowed </li></ul>
• 29. Pseudographs <ul><li>Self loops are permitted. </li></ul>
• 30. <ul><li>Trees are special cases of graphs!! </li></ul>Trees vs graphs