CS61B Guide
  • This site is now deprecated
  • Object Oriented Programming
    • Inheritance
    • Access Control
    • Dynamic Method Selection
    • Java Objects
    • Generic Types
  • Asymptotics
    • Asymptotic Analysis Basics
    • Amortization
    • Asymptotics Practice
  • Abstract Data Types
    • Collections
      • Arrays
      • Linked Lists
      • Sets
      • Stacks and Queues
    • Binary Trees
      • Heaps
      • Balanced BSTs
      • Tries
    • Graphs
    • Hashing and Hash Tables
    • Union Find (Disjoint Sets)
    • Comparables and Comparators
  • Sorting
    • Sorting
  • Algorithms
    • Minimax Algorithm
    • Searching
      • Binary Search
      • Depth First Search (DFS)
      • Breadth First Search (BFS)
    • Shortest Paths
      • Dijkstra's Algorithm
      • A* Search
    • Minimum Spanning Trees
      • Prim's Algorithm
      • Kruskal's Algorithm
  • Misc Topics
    • Modular Arithmetic and Bit Manipulation
    • Exceptions
    • More Resources
Powered by GitBook
On this page
  • Introduction
  • Graph Properties
  • Graph Queries
  • More on Graphs

Was this helpful?

  1. Abstract Data Types

Graphs

PreviousTriesNextHashing and Hash Tables

Last updated 5 years ago

Was this helpful?

Introduction

Graphs are simply a collection of vertices connected by edges. They're very similar to trees, but are much more versatile and don't require hierarchical relationships like trees do.

For most purposes, we will be working with simple graphs that follow two rules:

  • There are no loops (a connection of a node to itself).

  • There are no parallel edges (two edges that connect the same two vertices).

Graph Properties

Graphs can be described by some properties that they could have. Here are the important ones:

A graph can be directed if edges are arrows and have a direction, or undirected if you can cross edges in any direction.

A graph is cyclic if the edges form a loop, or acyclic if there are no loops (like in a tree).

Graphs can have edge labels if edges are numbered (great for distances). They can also have vertex weights if vertices are numbered (great for priorities or costs).

Graphs are connected if all of the vertices are connected with edges, such that you can freely move from one vertex to any other vertex.

Graph Queries

Here are some cool things you can do with graphs:

  • Is there a path between two vertices? (s-t path)

  • What is the shortest route between two vertices? (shortest s-t path)

  • Are there cycles? (cycle detection)

  • Can you visit each vertex/edge exactly once? (Euler tour / Hamilton tour)

  • Is a graph connected? (connectivity problem)

  • Is a vertex that disconnects the graph when removed? (single point of failure / biconnectivity)

  • Are two graphs isomorphic?

  • Can a graph be drawn with no crossing edges? (planarity)

More on Graphs

, , , , , , , and all rely on graphs. Graphs are a super useful concept!!!

Depth First Search (DFS)
Breadth First Search (BFS)
Minimum Spanning Trees
Shortest Paths
Dijkstra's Algorithm
A* Search
Prim's Algorithm
Kruskal's Algorithm
A very simple graph.
Don't make these graphs pls. Keep life simple!
Direction vs. Cycles
Edge labels vs. Weights