Graphs MADE BY Zulkifal Yousaf [email_address]
Graph <ul><li>A data structure that consists of a set of nodes ( vertices ) and a set of edges that relate the nodes to ea...
<ul><li>Node:   </li></ul><ul><li>Vertex point junction  </li></ul><ul><li>Edge:   </li></ul><ul><li>Give path.  </li></ul...
Undirected graphs When the edges in a graph have no direction, the graph is called  undirected <ul><ul><li>edges do not ha...
<ul><li>When the edges in a graph have a direction, the graph is called  directed  (or  digraph )   </li></ul>Directed gra...
Graph implementation <ul><li>Array-based implementation </li></ul><ul><ul><li>A 1D array is used to represent the vertices...
Array-based implementation
<ul><li>Linked-list implementation </li></ul><ul><ul><li>A 1D array is used to represent the vertices  </li></ul></ul><ul>...
Linked-list implementation
<ul><li>Traversing a graph </li></ul><ul><li>Where to start? </li></ul><ul><li>Will all vertices be visited? </li></ul><ul...
Graph searching   <ul><li>Depth-First-Search (DFS) or Breadth-First-Search (BFS)   </li></ul>
depth first traversal <ul><li>depthFirstTraversal (v) </li></ul><ul><li>visit v </li></ul><ul><li>mark v as visited </li><...
<ul><li>Set found to false </li></ul><ul><li>stack.Push(startVertex) </li></ul><ul><li>DO </li></ul><ul><li>stack.Pop(vert...
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  ...
start end (initialization)
 
 
Breadth-First-Searching (BFS) <ul><li>What is the idea behind BFS? </li></ul><ul><ul><li>Look at all possible paths at the...
breadth first traversal <ul><li>Breadth First Traversal (v) </li></ul><ul><li>put v in Q </li></ul><ul><li>while Q not emp...
<ul><li>BFS can be implemented efficiently using a  queue </li></ul><ul><ul><li>Set found to false </li></ul></ul><ul><ul>...
start end (initialization)
next:
 
EXAMPLES OF GRAPHS IN OUR DAILY LIFE FACEBOOK ,TWITTER,OTHER SOCAIL NETWORKS
some problems that can be represented by a graph <ul><li>computer networks </li></ul><ul><li>airline flights </li></ul><ul...
Types of Graphs <ul><li>SIMPLE GRAPH </li></ul><ul><li>At most one edge between any pair of nodes. </li></ul>
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,...
Multigraph <ul><li>Multiple edges between nodes are allowed </li></ul>
Pseudographs <ul><li>Self loops are permitted.  </li></ul>
<ul><li>Trees are special cases of graphs!!   </li></ul>Trees vs graphs
Upcoming SlideShare
Loading in...5
×

Graphs

2,046

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

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

No notes for slide

Transcript of "Graphs"

  1. 1. Graphs MADE BY Zulkifal Yousaf [email_address]
  2. 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. 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. 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. 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><V1, V2> and <V2, V1> are different edges </li></ul></ul></ul>
  6. 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. 7. Array-based implementation
  8. 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>
  9. 9. Linked-list implementation
  10. 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. 11. Graph searching <ul><li>Depth-First-Search (DFS) or Breadth-First-Search (BFS) </li></ul>
  12. 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. 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 &quot;Path does not exist&quot; </li></ul>Depth-First-Search (DFS)
  14. 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. 15. start end (initialization)
  16. 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>
  17. 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>
  18. 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 &quot;Path does not exist&quot;  
  19. 21. start end (initialization)
  20. 22. next:
  21. 24. EXAMPLES OF GRAPHS IN OUR DAILY LIFE FACEBOOK ,TWITTER,OTHER SOCAIL NETWORKS
  22. 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>
  23. 26. Types of Graphs <ul><li>SIMPLE GRAPH </li></ul><ul><li>At most one edge between any pair of nodes. </li></ul>
  24. 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>
  25. 28. Multigraph <ul><li>Multiple edges between nodes are allowed </li></ul>
  26. 29. Pseudographs <ul><li>Self loops are permitted. </li></ul>
  27. 30. <ul><li>Trees are special cases of graphs!! </li></ul>Trees vs graphs

×