1 / 32

Introduction To Graphs

Introduction To Graphs. In this section of notes you will learn about a new ADT: graphs. Start?. Start?. Start?. Start?. Start?. Graphs Are Related To Trees. Like a tree a graph consists of nodes (vertex) and arcs (edges) that connect the nodes

yakov
Download Presentation

Introduction To Graphs

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. Introduction To Graphs In this section of notes you will learn about a new ADT: graphs.

  2. Start? Start? Start? Start? Start? Graphs Are Related To Trees • Like a tree a graph consists of nodes (vertex) and arcs (edges) that connect the nodes • Unlike a tree there is no “up/down” direction (no parent-child relation), there is no root node Fort McMurray Edmonton Calgary Banff Lethbridge

  3. Graph Terminology • Adjacent nodes • Cycle • Acyclic graph • Sub-graph • Connected/disconnected graphs • Complete graphs • Directed/undirected graphs • Weighted graphs

  4. Adjacent Nodes • Nodes are adjacent if they are connected by an edge A E B D C

  5. Cycle • A path that begins and ends with the same node A E B D C

  6. Acyclic Graph • Has no cycles A E B D C

  7. A E B D C Sub-Graph • A portion of a graph that is also a graph A E D

  8. Connected Graphs • You can go from any node to any other node by following the edges Note: It is not a requirement for connected graphs to have edges from every pair of nodes

  9. Disconnected Graphs • Some nodes are unreachable because there is no edge that connects them with the rest of the graph

  10. Complete Graphs • Every pair of nodes has an edge between them (every node is directly connected to every other node)

  11. Directed Graphs • Traversal between nodes is not guaranteed to be symmetric • E.g., map information that represents one way streets

  12. Undirected Graphs • Each connection is symmetric (a connection in one direction guarantees a connection in other direction)

  13. Weighted Graph • Shows the cost of traversing an edge • Costs of traveling between cities • Distance in kilometers • Travel time in hours • Dollar cost of a taxi • Etc. Edmonton 300 100 Banff Calgary 150 Lethbridge

  14. Root Comparing Trees And Graphs Again • A Tree is A More Specific Form Of Graph • A typical1 tree is a graph that has the following characteristics • It is connected • It has no cycles1 • There is an up/down direction (there is a parent-child relation between nodes) • One node is treated as the top (the root node has no parent node) 1 The type of tree that you were required to implement was somewhat rare

  15. Graph Implementations Graph ADT (general concept) Data structure (specific) Adjacency matrix (Array) Adjacency list (Linked list)

  16. Adjacency Matrix: Array Implementation A B C D E F G H I G D A A B C D E F G H I H B E I C F ADT: Graph • Data structure: A 2D square array • No rows = no columns • = no. of nodes in the graph

  17. Possible Array Implementations A B C D E F G H I A B C D E F G H I A B C D E F G H I A B C D E F G H I A 2D array of boolean values A 2D array of integer values

  18. A Linked List Implementation Of A Graph A D G B E H C F I

  19. A D G B E H C F I The List Of Edges Must Be Dynamic1 BDE E B G FH CH H I F 1 Some sort of resizable list is needed e.g., a linked list or an array that can change in size

  20. An Outline For A Node class Node { private dataType data; private boolean visited; Dynamic list of connections; : : : }

  21. Graph Traversals • Breadth first • Depth first

  22. N 1st First level of references L1(a) L1 (b) 3rd 2nd Second level of references L2 (a) 4th Breadth-First Traversals • Visit a node (N) • Visit all of the nodes that node N refers to before following the second level of references :

  23. Algorithm For Breadth-First Traversals • In a fashion that is similar to breadth first traversals for trees, a queue is employed to store all the nodes that are adjacent to the node that is currently being visited. breadthFirst (node) { Queue nodeList = new Queue () Node temp Mark node as visited and display node nodeList.enqueue(node)

  24. Algorithm For Breadth-First Traversals (2) while (queue.isEmpty() == false) { temp = nodeList.dequeue () for (each unvisisted node uNode that is adjacent to temp) { Mark uNode as visited display uNode nodeList.enqueue(uNode) } } }

  25. First level Third level Fourth level First Example Of A Breadth First Traversal v Starting point w x u Second level t q s r

  26. Second Example Of A Breadth-First Traversal Starting point A D G B E H C F I Q: What order do you get for a breadth-first traversal if the starting point is node E?

  27. N 1st L1(a) 2nd L2 (a) 3rd Depth-First Traversals • Visit a node • Completely follow the series of references for a chain of nodes before visiting the second reference for that node L1 (b) :

  28. Algorithm For Depth-First Traversals • Typically recursion is used (requires backtracking and the use of the system stack). • If a loop is used then the programmer must create and manage his or her own stack. depthFirst (node) { Display node Mark node as visited for (each unvisited node (uNode) that is adjacent to node) depthFirst (node) }

  29. First Example Of A Depth First Traversal v Starting point w u x t q s r

  30. Second Example Of A Depth-First Traversal Starting point A D G B E H C F I Q: What order do you get for a depth-first traversal if the starting point is node E?

  31. You Should Now Know • What is a graph • Common graph definitions • What are the different ways in which graphs can be implemented • How do breadth-first and depth-first traversals work

  32. Sources Of Lecture Material • “Data Structures and Abstractions with Java” by Frank M. Carrano and Walter Savitch • “Data Abstraction and Problem Solving with Java: Walls and Mirrors” by Frank M. Carrano and Janet J. Prichard • CPSC 331 course notes by Marina L. Gavrilova http://pages.cpsc.ucalgary.ca/~marina/331/

More Related