Download presentation
Presentation is loading. Please wait.
1
Refresh and Get Ready for More
Graphs Refresh and Get Ready for More Copyright © – Curt Hill
2
Copyright © 2001-2016 – Curt Hill
Introduction We have seen some of this in the tree presentation We will quickly review the definitions Then some graph problems and algorithms Another presentation will consider algorithms in more detail Copyright © – Curt Hill
3
Mathematical definitions
Nodes or Vertices Arcs or Edges Indegree Outdegree Directed or digraph Undirected Network 2 1 3 6 4 5 Copyright © – Curt Hill
4
Copyright © 2001-2016 – Curt Hill
Undirected graphs Two nodes are adjacent if an arc connects them A path is a collection of arcs leading from one node to another A cycle is a path of at least three arcs that starts and ends on the same node A graph is connected if there is a path from any node to any other A disconnected graph contains components Copyright © – Curt Hill
5
Copyright © 2001-2016 – Curt Hill
Directed graphs Also has the notion of paths and cycles A strongly connected digraph has a path from every node to every other node A weakly connected digraph has a path from every node to every other node, only if converted to an undirected graph Copyright © – Curt Hill
6
Mathematical definitions
Two components Strongly connected Weakly connected 2 1 3 6 4 5 9 8 7 Copyright © – Curt Hill
7
Copyright © 2001-2016 – Curt Hill
Real Graphs Airline/bus/train routes Circuit board component connections Molecule diagrams People with a “works with” relationship Rooms of a building Flow graphs of programs Moves in games or puzzles Copyright © – Curt Hill
8
Copyright © 2001-2016 – Curt Hill
Flow Graphs 2: a = b * 2; 3: while(b < 4) { 4: c += a/2 + b--; 5: if(a<0) 6: a = -a*2 + 1; 7: } 8: a = 0; 2 3 4 5 6 7 8 Copyright © – Curt Hill
9
Copyright © 2001-2016 – Curt Hill
Set Representation Set of destinations Each node contains a set of those nodes that are connected by an arc 7: {8, 9} 8: {9} Sets of ordered pairs First is source, second is destination {(7,8),(7,9),(8,9)} 9 8 7 Copyright © – Curt Hill
10
Copyright © 2001-2016 – Curt Hill
Adjacency Matrix Matrix contains Booleans or weights A sparse matrix can be a list of lists 7 8 9 1 2 2 9 8 1 7 2 Copyright © – Curt Hill
11
Traversing/Searching Graphs
The big danger is cycles Three methods are related Depth first Breadth first Best first Copyright © – Curt Hill
12
Copyright © 2001-2016 – Curt Hill
Topological Sort Suppose a directed graph with no cycles A topological order of this graph has all nodes in sequential order such that no node is listed until all predecessors are listed Not always unique 1,2,3,4,5,6 1,3,2,4,5,6 1,2,3,5,4,6 1,3,2,5,4,6 Among others 4 2 6 1 5 3 Copyright © – Curt Hill
13
Copyright © 2001-2016 – Curt Hill
Spanning Trees A tree that starts at a particular node and contains each node of a graph but has only a subset of the arcs Spanning trees are not usually unique If the arcs have a cost function A minimal spanning tree has the lowest sum of costs Prim’s Algorithm will find the minimal spanning tree Copyright © – Curt Hill
14
Traveling Salesman’s Problem
There are a number of cities that are connected by roads A traveling salesman must visit them all What is the shortest route? This is a classic intractable problem The solution takes O(en)time Copyright © – Curt Hill
15
Copyright © 2001-2016 – Curt Hill
Finally Graphs provide several problems Although a tree is a graph it is very manageable Graphs provide problems with representation Each application may need a different Sometimes the graph is not represented completely at all The cycle issue provides problems for searching and traversal Copyright © – Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.