Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comp 245 Data Structures Graphs.

Similar presentations


Presentation on theme: "Comp 245 Data Structures Graphs."— Presentation transcript:

1 Comp 245 Data Structures Graphs

2 Introduction to the Graph ADT
A graph can be defined as a set of points connected by lines. The graph ADT can be defined this way also; the set of points will be called nodes or vertices and the set of lines will be called edges. A tree is a graph but a graph is not necessarily a tree! A tree is a subset of a graph!

3 An Abstract View of Graphs
A tree imposes a hierarchical view of data, a graph does not have to impose any kind of hierarchy.

4 Graph Terminology Undirected Directed
If a graph is undirected an edge essentially goes in both directions, in a directed graph an edge travels in one direction.

5 Graph Terminology Connected Disconnected
A graph is connected if every node can reach every other node!

6 Graph Terminology Complete
A graph is complete if each node can reach every other node directly.

7 Graph Terminology Other Terms Weighted Path Cycle Adjacent C B A F D
20 10 100 40 25 30 Other Terms Weighted Path Cycle Adjacent

8 Implementing a Graph Adjacency Matrix
B F C E G H Adjacency Matrix 1 2 3 4 5 6  0  1 1   Map A B C E F G H 1 2 3 4 5 6

9 Implementing a Graph Adjacency List
B F C E G H A B C E F G H

10 Graph Traversals A graph traversal can start at any node
How many nodes can it reach; this is a measure of “connectedness” Utilize the “backtracking” idea to do a traversal

11 Can We Connect to a Node? (Traverse from a given node)
B F C E G H From E can we get to H (E->H)? PUSH all moves from E onto stack ZERO OUT E column (Breadcrumbing) If stack is EMPTY, node is disconnected Otherwise, POP stack to get next move If this is node desired then finished, otherwise repeat process 1 2 3 4 5 6  0  1 1   A B C E F G H 1 2 3 4 5 6

12 Dijkstra’s Shortest Path Algorithm
Used with a weighted, possibly directed graph. Will calculate the shortest path, least cost, or minimum distance from an “origin” node in a graph to every other node it is “connected” to in the graph.

13 Dijkstra’s Algorithm A is “origin” node
B C D E F G H 3 1 2 4 5

14 Dijkstra’s Algorithm Shortest Path Table
V Known dv Pv A 1 - B 3 C 4 D 2 E F G 5 H 6 A B C D E F G H 3 1 2 4 5 V – visited node Known – indicates we “know” the shortest path dv – distance (weight, cost, etc…) from “origin” node to this node Pv – previous node

15 Dijkstra’s Algorithm Shortest Path Table - Interpreting
V Known dv Pv A 1 - B 3 C 4 D 2 E F G 5 H 6 A B C D E F G H 3 1 2 4 5 Path To: Distance Path B 3 A - B C 4 A - B - C D 2 A - D E A - E F A - D - F G 5 A - D - F - G H 6 A - D - F - H

16 Practice Create the Shortest Path Table
D C F G E 30 50 40 20 5 10 60

17 Practice Create the Shortest Path Table
V Known dv Pv A 1 - B 30 C 50 D 20 E 40 F G A B D C F G E 30 50 40 20 5 10 60 Path To: Distance Path B 30 A - B C 50 A - C D 20 A - D E 40 A - B - E F A - D - F G A - D - G


Download ppt "Comp 245 Data Structures Graphs."

Similar presentations


Ads by Google