1 / 95

Graph

Graph. Definition. G= (V,E) where V is a set of nodes and E is a set of Edges Let |V| be # nodes and let |E| be # Edge in the graph Direct Graph VS Undirect Graph. Directed graph. Undirected graph. Representation. Adjacency matrix For Dense graph

silvio
Télécharger la présentation

Graph

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graph

  2. Definition • G= (V,E) whereV is a set of nodes and E is a set of Edges • Let|V| be # nodes and let|E|be #Edge in the graph • Direct Graph VS Undirect Graph

  3. Directed graph Undirected graph

  4. Representation • Adjacency matrix • For Dense graph • Use matrix or array of size|V|x|V| โwhere each a[i,j]

  5. Node 0 1 2 3 Adjacency Matrix 4

  6. Node Adjacency Matrix in C int adj_mat[MAXNODES][MAXNODES] = { {0, 1,1,0,1}, {0, 0,1,1,0}, {0, 0,0,0,1}, {0, 1,0,0,1}, {0, 1,0,0,0}};

  7. 0 1 3 2 4 Node

  8. Node 0 1 2 3 4

  9. Representation • Adjacency list • For Sparse graph • Use array of Adjacency list of size |V|

  10. 1 2 4 2 3 0 4 1 4 1 1 2 Adjacency List 3 4

  11. 1 2 4 2 3 4 1 4 1 typedef struct edge_list { int nodeto; struct edge_list *next; } EDGE; typedef struct node { int nodefrom; EDGE *adj; } NODE; Adjacency List in C

  12. nodeto next EDGE nodefrom adj nodefrom adj NODE node_array[0] node_array[1] NODE nodefrom adj

  13. 0 1 2 3 4 Finding root of Graph fromAdjacency Matrix Root of Directed Graph is a set of nodes that have no incoming edges. Root ={0} Child Node of0 ...... Parent Node of2 .....

  14. 0 1 2 3 4 Finding root of Graph fromAdjacency List

  15. void find_root(NODE node_list[]) { int i,j,r=0; int isroot=0, node; EDGE *ptr; int root_array[MAXNODES]; for ( i=0; i < MAXNODES; i++) { node = node_list[i].nodefrom; isroot = 1; for (j=0; j < MAXNODES && isroot; j++) { ptr = node_list[j].adj; while (ptr != NULL && isroot) { if (ptr->nodeto == node) { isroot = 0; break; } ptr = ptr->next; } } if (isroot) root_array[r++] = node; } printf("roots are "); for (i=0; i < r; i++) printf("%d ",root_array[i]); }

  16. Graph Traversal • Depth First Search • Breadth First Search

  17. Depth First Traversal • Traverse in depth first manner • Use recursive algorithm • Use stack to help remember previous path

  18. Outline of depth-first search Depth_first() { for all u in G u.visit = false; for all u in G if (u.visit == false) traverse(u); } traverse(v) { v.visit = true; for all w adjacent to v if (w.visit = false ) traverse(w); }

  19. start

  20. start 1 2 0 4 3 8 5 6 7 Time complexity DFS is Depth-first traversal

  21. Breadth First Traversal • Traverse the graph in breadth-first manner • Use queue to store nodes in the same level

  22. Outline of breadth-first search breadh_first() { Queue q; for all u in G u.visit = false; for all u in G if (u.visit == false) { enqueue(q,u); while (not_empty(q)) { w = dequeue(q); if (w.visit ==false) { w.visit = true; for all x adjacent to w enqueue(q,x); } } } }

  23. start enqueue(Q,B), enqueue(Q,E),enqueue(Q,D) Q={B,E,D} 0 A B C Q={A} E D F G H I while (not_empty(q)) { w = dequeue(q); if (w.visit ==false) { w.visit = true; for all x adjacent to w enqueue(q,x); } } Breadth-first traversal

  24. Q={B,E,D} B = dequeue(Q) start enqueue(Q,C) Q={E,D,C} 0 1 A B C E D F G H I while (not_empty(q)) { w = dequeue(q); if (w.visit ==false) { w.visit = true; for all x adjacent to w enqueue(q,x); } } Breadth-first traversal

  25. start 0 1 A B C Q={E,D,C} E= dequeue(Q) Q={D,C} 2 E D F enqueue(Q,G) Q={D,C,G} G H I Breadth-first traversal

  26. start 0 1 A B C Q={D,C,G} D= dequeue(Q) Q={C,G} 2 3 E D F G H I Breadth-first traversal

  27. start Q={C,G} C= dequeue(Q) Q={G} 4 0 1 A B C enqueue(Q,F) Q={G,F} 2 3 E D F G H I Breadth-first traversal

  28. start 4 0 1 A B C 2 3 E D F Q={G,F} G= dequeue(Q) Q={F} enqueue(Q,H) Q={F,H} 5 G H I Breadth-first traversal

  29. start 4 0 1 A B C Q={F,H} F= dequeue(Q) Q={H} 2 6 3 E D F 5 G H I Breadth-first traversal

  30. start 4 0 1 A B C 2 6 3 E D F Q={H} H= dequeue(Q) Q={} 7 5 G H I enqueue(Q,I) Q={I} Breadth-first traversal

  31. start 4 0 1 A B C 2 6 3 E D F 7 8 Q={I} I= dequeue(Q) Q={} 5 G H I Breadth-first traversal

  32. start 0 1 4 2 3 6 8 7 5 Time complexity BFS is Breadth-first traversal

  33. Topological Sort • Order nodes in graph • Mayuse Depth-first or Breadth – first search Time complexity is

  34. Use Breadth-first search. We get order {A,B,C,D} B A C D Topological Sort

  35. start A B C E D F G H I Exercise -What are node orders if breadth-first traversal?

  36. start A B C E D F G H I Exercise - What are node orders if depth-first traversal?

  37. What is the topological order of these nodes? 0 1 2 3 Exercise 4

  38. Weighted Graph • G= (V,E,W) whereV is a set of nodes and E is a set of Edges • W is function mapping from edges to integers representing weight of the corresponding edge

  39. 1 1 3 1 2 2 1 1 1 2 Directed graph Undirected graph

  40. Shortest Path in Directed Graphs • Shortest path weight จาก u ไป v คือ andshortest path fromu tov isany path where If there ispath fromu tov otherwise

  41. Shortest Path in Directed Graphs • LetG=(V,E,w) where weight of path as

  42. Shortest Path in Directed Graphs • Single source shortest path finds theshortest path fromsingle source to all nodes in graph • NOTE:w(u,v) may be positive or negative

  43. Dijkstra’s Algorithm • Finding solution forSingle source shortest path problem fornon-negative weight cycle • Negative weight cycle causesno-shortest path fromsource to thedestination

  44. a b -4 When there isnegative weight cycle 3 -1 4 3 6 s c d g 5 8 0 5 11 -3 2 7 e f 3 -6 Froms toc there are many path<s,c>, <s,c,d,c>,<s,c,d,c,d,c>… If cycle <c,d,c> has w=6-3=3 which is positive we still can findshortest path which is 5

  45. a b -4 When there isnegative weight cycle 3 -1 4 3 6 s c d g 5 8 0 5 11 -3 2 7 3 e f - -6 Froms to e, there are paths<s,e>, <s,e,f,e>,<s,e,f,e,f,e>… But cycle <e,f,e> hasw=3-6=-3 which is negative so we get shortest path as -

More Related